Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

vector::const_reference

A type that provides read-only access to elements in a vector.

typedef typename Allocator::const_reference const_reference;

A type const_reference cannot be used to modify the value of an element. You can use const_reference on non-const elements but you must use const_reference on const elements.

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

   const vector <int> v2 = v1;
   const int &i = v2.front( );
   const int &j = v2.back( );
   cout << "The first element is " << i << endl;
   cout << "The second element is " << j << endl;
   
   // The following line would cause an error as v2 is const
   // v2.push_back( 30 );
}
The first element is 10
The second element is 20

Header: <vector>

Namespace: std

Show:
© 2015 Microsoft