Iterator and Const Iterator Comparison
Developer based in London. Mainly programming in C++ now, but with some skills in Python and C#. About 19 years experience working on Financial Software - may look to branch out at some point.
I managed to get hold of a copy of Scott Meyer's Effective STL. Originally printed in 2001, this book contains all sorts of insights that I'm slowly working my way through.
Item 26, "Prefer iterator to const_iterator" states that, in some implementations, you cannot compare an iterator to a const_iterator. I have a memory of doing this in the past, so fired up Visual Studio to check the current situation.
std::vector<int> vec;
std::vector<int>::iterator it = vec.begin();
std::vector<int>::const_iterator cit = vev.cbegin();
if(it == cit && cit == it)
std::cout << "iterators equal" << std::endl;
This code compiles and runs as you would expect.
What about reverse_iterators? Conceivably you may wish to move front-to-back and back-to-front through a container, while checking that your iterators don't point to the same element.
If we add the following lines, the program will no longer compile:
std::vector<int>::reverse_iterator rit = vec.rbegin();
if(it == rit && rit == it) // Compile Error!
std::cout << "iterators equal" << std::endl;
In summary, using Visual Studio 2019, C++ 14 standard, we can compare iterators to their const counterparts, but we cannot compare iterators to reverse iterators.