std :: swap错误分配只读引用

use*_*934 0 c++ stl

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);
            }                
        }        
    }       
}
Run Code Online (Sandbox Code Playgroud)

我需要调换*it*itt,它给这个错误.

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'|
Run Code Online (Sandbox Code Playgroud)

编辑:此功能的目的是安排地图的元素.根据second.type

use*_*267 7

std::map对于键,迭代器总是不变的(考虑一下,如果更改键值,底层树可能会被破坏!),你可以交换值:

std::swap(it->second, itt->second);
Run Code Online (Sandbox Code Playgroud)

免责声明:这可能是您正在寻找的,也可能不是,因为我不知道交换的目的是什么.