vector::const_reference

一个类型,它提供对向量中元素的只读访问全项。

typedef typename Allocator::const_reference const_reference;

备注

const_reference 类型不能用于修改元素的值。 可在非常量元素上使用 const_reference,但必须在常量元素上使用 const_reference。

示例

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

要求

标头:<vector>

命名空间: std

请参见

参考

vector 类

标准模板库