I want to replace an element in a set.After searching in Google, I came across this function replace
, but when I used it, it gave me an error
no member named 'replace' in
What are other ways of updating or replacing an element in set?
Set elements are constant and may not be modified in place.
Modifying a set in Python We cannot access or change an element of a set using indexing or slicing. Set data type does not support it. We can add a single element using the add() method, and multiple elements using the update() method. The update() method can take tuples, lists, strings or other sets as its argument.
The value itself can identify each element in a set, i.e., they act as keys themselves. We can insert elements in or remove elements from a set in C++ but cannot modify them since the values become constant once stored in the set.
The standard solution to check for existence of an element in the set container ( std::set or std::unordered_set ) is to use its member function find() . If the specified element is found, an iterator to the element is returned; otherwise, an iterator to the end of the container is returned.
You have got to erase()
one element and insert()
the new one. replace
is not defined for std::set
. See http://www.cplusplus.com/reference/set/set/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With