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.
这篇关于如何更改集合元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改集合元素?
基础教程推荐
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- c++ STL设置差异 2022-01-01
