MetallicPriest
MetallicPriest

Reputation: 30765

Writing elements of std::map through iteration

I have written the following code...

  PagesMap::const_iterator end = pagesMap.end(); 
  for ( PagesMap::const_iterator it = pagesMap.begin(); it != end; ++it )
  {
    ....
    it->second = 0; // Here I get the error
    //pagesMap[it->first] = 0; 
  }

Now at line where I have it->second = 0;, I get...

error: assignment of data-member ‘std::pair::second’ in read-only structure

If I use the commented code below that line, it works, but It's my guess that it is not efficient. Is there an efficient way to achieve this?

Upvotes: 3

Views: 153

Answers (1)

trojanfoe
trojanfoe

Reputation: 122391

It's because you are using a const_iterator; try changing to an iterator instead.

Upvotes: 12

Related Questions