我使用polinoms並將它們作爲度和係數保存在std :: map中。下面的代碼片段:使用std :: map時只讀成員的錯誤遞減:: map
std::map<int,int> pol;
地圖充滿了數據,然後我就開始對其進行處理。
for(std::map<int,int>::iterator it = pol.begin(); it != pol.end(); it++) {
if(it->first != 0) {
it->second *= it->first;
it->first--;
}
else {
it->first = 0;
it->second = 0;
}
}
而且從它 - >序曲一開始,並進一步我得到非常大的量與像error: decrement of read-only member ‘std::pair<const int, int>::first’ it->first--; ^~
或error: assignment of read-only member ‘std::pair<const int, int>::first’ it->first = it->first - 1;
錯誤輸出爲什麼只讀?我該如何解決它?
$ g++ --version
g++ (Debian 6.3.0-5) 6.3.0 20170124
[強制鍵類型的std :: map不是const的可能的重複](http://stackoverflow.com/questions/6773734/force-key-type-of-a-stdmap-not-to- be-const) – nwp