简体   繁体   English

C ++:std :: vector []运算符

[英]C++: std::vector [] operator

Why std::vector has 2 operators [] realization ? 为什么std :: vector有2个operator []实现?

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

One for non-const vector object, and the other for const vector object. 一个用于非const矢量对象,另一个用于const矢量对象。

void f(std::vector<int> & v1, std::vector<int> const & v2)
{
   //v1 is non-const vector
   //v2 is const vector

   auto & x1 = v1[0]; //invokes the non-const version
   auto & x2 = v2[0]; //invokes the const version

   v1[0] = 10; //okay : non-const version can modify the object
   v2[0] = 10; //compilation error : const version cannot modify 

   x1 = 10; //okay : x1 is inferred to be `int&`
   x2 = 10; //error: x2 is inferred to be `int const&`
}

As you can see, the non-const version lets you modify the vector element using index, while the const version does NOT let you modify the vector elements. 如您所见,非const版本允许您使用索引修改vector元素,而const版本不允许您修改vector元素。 That is the semantic difference between these two versions. 这就是这两个版本之间的语义差异。

For more detail explanation, see this FAQ: 有关详细说明,请参阅此常见问题解答:

Hope that helps. 希望有所帮助。

To make this differentiation possible: 为了实现这种区分:

// const vector object, cannot be modified
// const operator[] allows for this
int get_value(const std::vector<int>& vec, size_t index)
{
   return vec[index];
}

// non-const vector object can be modified
// non-const operator[] allows for this
void set_value(std::vector<int>& vec, size_t index, int val)
{
   vec[index] = value;
}

std::vector<int> values;
values.push_back(10);
values.push_back(20);

set_value(values, 0, 50);
get_value(values, 0);

one so that you can modify and read from a (non const) vector 一,以便您可以修改和读取(非常量)向量

void foo( std::vector<int>& vector )
{
    // reference operator[]( size_type );
    int old = vector[0];
    vector[0] = 42;
}

one so that you can read from a const vector 一个,这样你就可以读取const向量

void foo( std::vector<int> const& vector )
{
    //const_reference operator[]( size_type ) const;
    int i = vector[0];

}

One of the two overloads allows you to retrieve a const reference to an element of a vector accessed through a const variable. 两个重载之一允许您检索对通过const变量访问的向量元素的const引用。 The other one allows you to obtain a non- const reference to an element of a vector accessed through a non- const variable. 另一个允许您获取对通过非const变量访问的向量的元素的非const引用。

If you didn't have the const version, you wouldn't be allowed to compile the following for instance: 如果您没有const版本,则不允许编译以下内容:

void f(vector<int> const& v)
{
    cout << v[0]; // Invokes the const version of operator []
}

In the following example, instead, the non-const version is invoked, which returns a non- const reference to the first element in the array and allow, for instance, to assign a new value to it: 在下面的示例中,相反,调用非const版本,它返回对数组中第一个元素的非const引用,并允许,例如,为其分配一个新值:

void f(vector<int>& v)
{
    v[0] = 1; // Invokes the non-const version of operator[]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM