How to change a set item?

I want to change an element in set , so I used set<T>::iterator . However, the compiler claims to be a "const element". Then I realized that set<T>::iterator is const_iterator ...

So how can I change an item? Delete it and then insert a new one?

+6
source share
2 answers

set elements will be sorted in sort order. If you are allowed to change an item, this sort order cannot be saved. Therefore, you cannot change the item. You need to erase the existing item and insert a new one.

+20
source

EDIT: You cannot add an item to a specific place in a set. the set should be in sorted order, what operations do you perform. Thus, you need to delete a specific element and insert a new element so that the dialing order is not lost.

Also read more about set !

0
source

Source: https://habr.com/ru/post/910164/


All Articles