简体   繁体   English

C中迭代器的const向量

[英]const vector of iterators in c++

If I have a vector that contains iterators of another vector. 如果我有一个包含另一个向量迭代器的向量。

For example : 例如 :

vector<vector<int>::iterator> vec;

what happens when passing this vector in const refrence? 在常量引用中传递此向量会发生什么?

For example: 例如:

void fun(const vector<vector<int>::iterator> &vec);

Are the elements of vec inside function fun const_iterator or iterator? Vec内部函数的元素是否有趣const_iterator或Iterator? can they be modified 他们可以修改吗

Thanks in advance! 提前致谢!

No, they aren't const_iterator . 不,他们不是const_iterator Chiefly because const_iterator is a user defined type that is related to iterator via a user defined conversion, and not in fact a const qualified iterator . 主要是因为const_iterator是通过用户定义的转换与iterator相关的用户定义类型,而不是实际上是const限定的iterator (1) (1)

What you access inside the function through the vector are const iterator& . 通过向量在函数内部访问的是const iterator&

So you can use those to modify the elements referred by them. 因此,您可以使用那些元素来修改它们引用的元素。

The best analogy to this is that const_iterator is to iterator , what const T* is to T* . 最好的类比是const_iteratoriteratorconst T*T* And what you have inside your function is akin to T * const . 函数内部具有的类似于T * const


(1) On paper, that is. (1)在纸上,即。 For vectors it may very well be plain pointers in optimized code. 对于向量,它很可能是优化代码中的普通指针。 In which case the analogy becomes the reality. 在这种情况下,类比变为现实。

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

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