Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ operator[] operator

C++ operator[] operator

C++ Container Class The C++ container class


Among all ways to access array elements,operator[] is the most commonly used and most direct one, just like accessing a normal array.

operator[] is the subscript operator of the container class, used toreturn the element at the specified position, but does not perform bounds checking. Its usage is exactly the same as a normal C array.

operator[] provides array-style random access, allowing you to quickly access elements at any position.

Word meaning: operator[] is the subscript operator; the square brackets are the symbol of array access.


Basic syntax and parameters

operator[] is a member function of the container class; just use it like an array.

Syntax format


reference operator[](size_type pos);
const_reference operator[](size_type pos) const;

Parameter Description

- Type: `size_type` (unsigned integer type, usually `size_t`) - Description: The position (index) of the element to access. Indexing starts at 0, and the maximum valid index is `size() - 1`.

Function description


Example

Let us thoroughly master operator[] through a series of examples from simple to complex.

Example 1: Basic usage - accessing elements

Example

#include

#include

int

main

(

)

{

   

// 1. Create and initialize an array

    std

::

array

<
int
,
5
numbers
=
{
10
,
20
,
30
,
40
,
50
}

;

    std

::

cout

<<

"array size is: "

<<

numbers.

size

(

)

<<

std

::

endl

;

   

// 2. Use operator[] to access elements

    std

::

cout

<<

"First element [0]: "

<<

numbers

[

0

]

<<

std

::

endl

;

    std

::

cout

<<

"Second element [1]: "

<<

numbers

[

1

]

<<

std

::

endl

;

    std

::

cout

<<

"Third element [2]: "

<<

numbers

[

2

]

<<

std

::

endl

;

    std

::

cout

<<

"Last element [4]: "

<<

numbers

[

4

]

<<

std

::

endl

;

   

// 3. Use a loop to access all elements

    std

::

cout

<<

"All elements: "

;

   

for

(

size_t

i

=

0

;

i

<

numbers.

size

(

)

;

++

i

)

{

        std

::

cout

<<

numbers

[

i

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

array的大小是: 5
第一个元素 [0]: 10
第二个元素 [1]: 20
第三个元素 [2]: 30
最后一个元素 [4]: 50
所有元素: 10 20 30 40 50

Code analysis:

  1. numbers[0] returns the first element 10(indexing starts at 0).
  2. numbers[4] returns the last element 50(because size() is 5, and the valid indices are 0-4).
  3. Using a loop and operator[] can traverse all elements, with usage exactly the same as a C array.

Example 2: Modifying element values

operator[] returns a reference, so it can be used to modify the element's value.

Example

#include

#include

int

main

(

)

{

    std

::

array

<
int
,
3
numbers
=
{
10
,
20
,
30
}

;

    std

::

cout

<<

"Before modification: "

;

   

for

(

size_t

i

=

0

;

i

<

numbers.

size

(

)

;

++

i

)

{

        std

::

cout

<<

numbers

[

i

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

// Use operator[] to get a reference and modify the element

    numbers

[

0

]

=

100

;

    numbers

[

1

]

=

200

;

    numbers

[

2

]

=

300

;

    std

::

cout

<<

"After modification: "

;

   

for

(

size_t

i

=

0

;

i

<

numbers.

size

(

)

;

++

i

)

{

        std

::

cout

<<

numbers

[

i

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

修改前: 10 20 30
修改后: 100 200 300

Code analysis:

Example 3: Range-based for loop

can use a range-based for loop to traverse array.

Example

#include

#include

#include

int

main

(

)

{

    std

::

array

<

std

::

string

,

3

names

=

{

"Alice"

,

"Bob"

,

"Charlie"

}

;

    std

::

cout

<<

"All elements: "

;

   

for

(

const

auto

&

name

:

names

)

{

        std

::

cout

<<

name

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

所有元素: Alice Bob Charlie

Code analysis:

Example 4: Computing the vector dot product

Use operator[] implements vector operations.

Example

#include

#include

// Compute the dot product of two vectors

int

dotProduct

(

const

std

::

array

<
int
,
3
&
v1,
const
std

::

array

<
int
,
3
&
v2
)
{
int
result
=
0

;

   

for

(

size_t

i

=

0

;

i

<

v1.

size

(

)

;

++

i

)

{

        result

=

v1

[

i

]

v2

[

i

]

;

   

}

   

return

result

;

}

int

main

(

)

{

    std

::

array

<
int
,
3
v1
=
{
1
,
2
,
3
}

;

    std

::

array

<
int
,
3
v2
=
{
4
,
5
,
6
}

;

   

int

result

=

dotProduct

(

v1, v2

)

;

    std

::

cout

<<

"Vector v1: "

;

   

for

(

size_t

i

=

0

;

i

<

v1.

size

(

)

;

++

i

)

{

        std

::

cout

<<

v1

[

i

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

    std

::

cout

<<

"Vector v2: "

;

   

for

(

size_t

i

=

0

;

i

<

v2.

size

(

)

;

++

i

)

{

        std

::

cout

<<

v2

[

i

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

    std

::

cout

<<

"Dot product: "

<<

result

<<

std

::

endl

;

   

// 14 + 25 + 3*6 = 4 + 10 + 18 = 32

   

return

0

;

}

Expected output:

向量 v1: 1 2 3
向量 v2: 4 5 6
点积: 32

Code analysis:


C++ Container Class The C++ container class