How to change a set element?(如何更改集合元素?)
问题描述
我想更改 set
中的元素,所以我使用了 set<T>::iterator
.但是,编译器认为元素是 const".然后我意识到 set<T>::iterator
是一个 const_iterator
...
I want to change the element in a set
, so I used set<T>::iterator
. However, the compiler argues "the element is const". Then I realized that set<T>::iterator
is a const_iterator
...
那么,我怎样才能改变元素呢?删除它然后插入一个新的?
So, how I can change the element? Erase it and then insert a new one?
推荐答案
set
的元素会按顺序排列.如果允许你修改一个元素,那么这个排序顺序就不能保持.因此,您不能修改该项目.您需要删除现有元素并插入一个新元素.
The elements of the set
will be in sorted order. If you are allowed to modify an element, then this sorting order can not be maintained. Hence you can not modify the item. You need to erase the existing element and insert a new one.
这篇关于如何更改集合元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改集合元素?


基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01