如何更改集合元素? [英] How to change a set element?

查看:40
本文介绍了如何更改集合元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于如何更改集合元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆