Passing an iterator and function vector

I have some function

void print_elem(const std::vector<int>::iterator it, const std::vector<int> &vec) {/*.....*/} 

If I do not want the vector to be a reference to the original object, I get copies of the vector. Why is this not true for an iterator? Why should the iterator also not be a reference?

For example, if I wanted to iterate over a vector, print each element and stop when I get to the end of the vector, if I don’t pass the link to the vector, the iteration just iterates through the first vector copy. But if I go through the link, then the iteration will go through the original vector object. But why is the iterator not copied the way a vector does without a link?

+6
source share
1 answer

The iterator models the pointer, and it most likely is either one or contains one that points to the vector or its contents. When you copy it, the copy is actually a different iterator, but it keeps the same value, so it still points to the same thing as the pointer.

+10
source

Source: https://habr.com/ru/post/952767/


All Articles