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.
这篇关于如何更改集合元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何更改集合元素?
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
