6

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?

Amr Eladawy
  • 4,193
  • 7
  • 34
  • 52
sasasasa
  • 137
  • 2
  • 8
  • 2
    There are probably a few duplicates of that question but you can't replace an item in a set. You have to remove the old item and add the new item. It's not too hard. – R Sahu May 27 '17 at 06:49
  • `c++` provide basic functionalities which can be used to form several others like `replace --> remove old + add new`. – jack jay May 27 '17 at 06:52
  • 2
    `After searching in Google, I came across this function replace` Seems weird to me, since it doesn't exist in this container. Where did you come across it? Did the site say it was defined for `std::set`? If so, that's an error and should be fixed. The reason that `replace` is not available here is that it doesn't actually make sense, since the container is automatically ordered by its elements, so you can't replace one in-place. – underscore_d May 27 '17 at 06:54
  • 1
    Possible duplicate of [C++ STL set update is tedious: I can't change an element in place](https://stackoverflow.com/questions/2217878/c-stl-set-update-is-tedious-i-cant-change-an-element-in-place) – underscore_d Sep 22 '18 at 13:42
  • *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve).* – jww Sep 22 '18 at 14:52

1 Answers1

8

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/

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
Gerriet
  • 1,302
  • 14
  • 20