我使用polinoms,并将它们作为度数和系数保存在std :: 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
这是只读的,因为如果允许您自由修改映射中的键,则会违反映射使用的数据结构的不变性(通常是一棵红黑树)。
您需要删除该元素,然后将其与递减后的值一起添加回去。这样可以确保该节点在树中的正确位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句