user2390934
user2390934

Reputation: 249

std::swap error assignment of read-only reference

void sort_this(std::map<BITMAP*,MAPS>::iterator  start,std::map<BITMAP*,MAPS>::iterator endd)
{
    for(auto itt=start;itt!=endd;itt++)
    {               
        for(auto it=start;it!=endd;it++)
        {        
            if(itt->second.type > it->second.type)
            {                                
                std::swap(*it,*itt);
            }                
        }        
    }       
}

i need to swap *it and *itt ,it gives this error.

files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\move.h|177|
error: assignment of read-only reference '__a'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\move.h|178|
error: assignment of read-only reference '__b'|

edit: purpose of this function is to arrange elements of a map. according to second.type

Upvotes: 0

Views: 536

Answers (1)

user657267
user657267

Reputation: 21000

std::map iterators are always constant with regard to the keys (think about it, if you change the key value the underlying tree risks being destroyed!), you can swap the values however:

std::swap(it->second, itt->second);

Disclaimer: this may or may not be what you are looking for, as I have no idea what the purpose of the swap is.

Upvotes: 7

Related Questions