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?
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.
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!