What is const_iterator
October 20th, 2008A const_iterator is not a const iterator. That is,
const vector<int>::iterator it;
and
vector<int>::const_iterator it;
are different. The first type (const iterator) does not allow any kind of modification of the iterator as well as the contents. That is, both
++it;
and
*it = 40 are illegal.
The second type(const_iterator) allows modification of the iterator, but prevents the contents from getting modified.
So,
++it is legal whereas *it = 40 is illegal.
#include<vector>
using namespace std;
int main()
{
vector<int> myVector;
myVector.push_back(10);
myVector.push_back(20);
vector<int>::const_iterator it = myVector.begin();
++it; // Legal
*it = 40; // Illegal
return 0;
}
