使用std :: map时只读成员的错误递减

凯基

我使用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
Brian Bi

这是只读的,因为如果允许您自由修改映射中的键,则会违反映射使用的数据结构的不变性(通常是一棵红黑树)。

您需要删除该元素,然后将其与递减后的值一起添加回去。这样可以确保该节点在树中的正确位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用具有只读成员的C#接口

来自分类Dev

使用std :: map :: at时出错

来自分类Dev

正确使用std :: map作为类成员

来自分类Dev

为只读成员赋值 - C++

来自分类Dev

C ++在std :: map <>中使用std :: set <>

来自分类Dev

在std :: map中使用std :: function

来自分类Dev

C ++在std :: map <>中使用std :: set <>

来自分类Dev

在std :: map中使用std :: function

来自分类Dev

std :: atomic作为类成员:使用boost / python.hpp时使用删除的函数错误

来自分类Dev

当我想要特定参数时,如何在std :: map中使用成员方法

来自分类Dev

当我想要特定参数时,如何在std :: map中使用成员方法

来自分类Dev

使用std :: string时出现分段错误

来自分类Dev

使用MinGW编译yaml-cpp时出错:错误:“ atoi”不是“ std”的成员

来自分类Dev

在std :: map中使用'auto'

来自分类Dev

在std :: map中使用decltype

来自分类Dev

使用std :: map的Miniheap Lambda

来自分类Dev

错误使用了std :: copy?

来自分类Dev

错误使用了std :: copy?

来自分类Dev

使用类的const成员的std :: deque :: erase编译错误

来自分类Dev

在std :: map中使用std :: any_of,std :: all_of,std :: none_of等

来自分类Dev

错误:使用gcc 4.8.2在命令行中编译时,“ to_string”不是“ std”的成员,可以在NetBeans中使用吗?

来自分类Dev

使用std shared_ptr作为std :: map键

来自分类Dev

使用std :: vector的std :: map初始化

来自分类常见问题

使用std :: ranges :: min在std :: map上投影

来自分类Dev

在std :: map中使用std :: reference_wrapper

来自分类Dev

使用std :: ranges :: min在std :: map上投影

来自分类Dev

如何使用std :: partial_sum并输出到std :: map?

来自分类Dev

在std :: map中使用std :: less无法编译

来自分类Dev

使用std :: vector的std :: map初始化

Related 相关文章

  1. 1

    如何使用具有只读成员的C#接口

  2. 2

    使用std :: map :: at时出错

  3. 3

    正确使用std :: map作为类成员

  4. 4

    为只读成员赋值 - C++

  5. 5

    C ++在std :: map <>中使用std :: set <>

  6. 6

    在std :: map中使用std :: function

  7. 7

    C ++在std :: map <>中使用std :: set <>

  8. 8

    在std :: map中使用std :: function

  9. 9

    std :: atomic作为类成员:使用boost / python.hpp时使用删除的函数错误

  10. 10

    当我想要特定参数时,如何在std :: map中使用成员方法

  11. 11

    当我想要特定参数时,如何在std :: map中使用成员方法

  12. 12

    使用std :: string时出现分段错误

  13. 13

    使用MinGW编译yaml-cpp时出错:错误:“ atoi”不是“ std”的成员

  14. 14

    在std :: map中使用'auto'

  15. 15

    在std :: map中使用decltype

  16. 16

    使用std :: map的Miniheap Lambda

  17. 17

    错误使用了std :: copy?

  18. 18

    错误使用了std :: copy?

  19. 19

    使用类的const成员的std :: deque :: erase编译错误

  20. 20

    在std :: map中使用std :: any_of,std :: all_of,std :: none_of等

  21. 21

    错误:使用gcc 4.8.2在命令行中编译时,“ to_string”不是“ std”的成员,可以在NetBeans中使用吗?

  22. 22

    使用std shared_ptr作为std :: map键

  23. 23

    使用std :: vector的std :: map初始化

  24. 24

    使用std :: ranges :: min在std :: map上投影

  25. 25

    在std :: map中使用std :: reference_wrapper

  26. 26

    使用std :: ranges :: min在std :: map上投影

  27. 27

    如何使用std :: partial_sum并输出到std :: map?

  28. 28

    在std :: map中使用std :: less无法编译

  29. 29

    使用std :: vector的std :: map初始化

热门标签

归档