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

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




The position of the vector element.

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

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.

When compiling with _SECURE_SCL 1 (controlled with _ITERATOR_DEBUG_LEVEL), a runtime error will occur if you attempt to access an element outside the bounds of the vector. See Checked Iterators for more information.

// 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;

The second integer of v1 is 20

Header: <vector>

Namespace: std

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft