What is const_iterator

A 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;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: