Idan
Idan

Reputation: 82

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? can they be modified

Thanks in advance!

Upvotes: 1

Views: 546

Answers (1)

No, they aren't 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. (1)

What you access inside the function through the vector are 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*. And what you have inside your function is akin to T * const.


(1) On paper, that is. For vectors it may very well be plain pointers in optimized code. In which case the analogy becomes the reality.

Upvotes: 7

Related Questions