2 out of 2 rated this helpful Rate this topic

vector::operator[]

Returns a reference to the vector element at a specified position.

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

Parameter

_Pos
The position of the vector element.

Return Value

If the position specified is greater than the size of the container, the result is undefined.

Remarks

If the return value of operator[] is assigned to a const_reference, the vector object cannot be modified. If the return value of operator[] is assigned to a reference, the vector object can be modified.

Example

// vector_op_ref.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;   
   vector <int> v1;

   v1.push_back( 10 );
   v1.push_back( 20 );

   int& i = v1[1];
   cout << "The second integer of v1 is " << i << endl;
}

Output

The second integer of v1 is 20

See Also

vector Class | vector Members

Did you find this helpful?
(1500 characters remaining)