First sentence: “ An iterator is an object that points to an element inside a container”
This is not the primary purpose of an iterator. Does anyone want to hazard a guess as to what an iterator’s primary purpose is? You? You? Bueller?
This is not the primary purpose of an iterator. Does anyone want to hazard a guess as to what an iterator’s primary purpose is? You? You? Bueller?
Take `std::copy_n` for example. You _could_ use this to copy from one vector to another:
std::vector<int> a = {1,2,3}, b = {0,0,0};
std::copy_n(a.begin(), a.size(), b.begin());
But iterators also allow the input and output collection types to be different: std::vector<int> a = {1,2,3};
std::array<int,3> b;
std::copy_n(a.begin(), a.size(), b.begin());
Or even for the output to not be associated with a container at all. std::vector<int> a = {1,2,3};
std::copy_n(a.begin(), a.size(), std::ostream_iterator<int>(std::cout, " "));Sometimes C pointers are used for iteration, but I would not say that is their primary purpose.
I really wonder why people put up with something named this way then. "That fool thinks iterators are for iteration!"
Clarity of abstraction and good naming are important, unless the practitioners are masochists.