How to change a set element?

2019-01-12 06:33发布

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?

2条回答
贼婆χ
2楼-- · 2019-01-12 07:00

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.

查看更多
forever°为你锁心
3楼-- · 2019-01-12 07:15

EDIT: You cant add an element to a specific location in set. the set should be in the sorted order whatever operations you do. So you have to erase the specific element and insert the new element so that the ordering of set isnt lost.

Also read more about set!

查看更多
登录 后发表回答