Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing elements of std::map through iteration

Tags:

c++

stl

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?

like image 539
MetallicPriest Avatar asked Feb 04 '26 06:02

MetallicPriest


1 Answers

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

like image 74
trojanfoe Avatar answered Feb 06 '26 20:02

trojanfoe