如何更改集合元素?

2022-01-17 00:00:00 set c++ const-iterator

我想更改 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.

相关文章