Is there a way to create a set with a new default custom comparator, and then every item that gets inserted in the set would be inserted based on the new comparator?
Asked
Active
Viewed 90 times
-1
-
Yes there is, as per the documentation for [std::set](https://en.cppreference.com/w/cpp/container/set). – Eljay Apr 03 '20 at 15:56
-
Is the custom comparator template parameter which @Eljay mentioned not what you are asking about? Please clarify. – einpoklum Apr 03 '20 at 15:59
-
Is your data type of Set is a `struct` or any other else? – Shudipta Sharma Apr 03 '20 at 16:06
-
this will help https://stackoverflow.com/questions/2620862/using-custom-stdset-comparator – parth_07 Apr 03 '20 at 17:51
-
1Does this answer your question? [Using custom std::set comparator](https://stackoverflow.com/questions/2620862/using-custom-stdset-comparator) – parth_07 Apr 03 '20 at 17:52
1 Answers
2
There is only one comparator instance for any set instance. All elements of the set are compared using that comparator. So, if you insert an element, the custom comparator of the set will be used.
![](../../users/profiles/2079303.webp)
eerorika
- 181,943
- 10
- 144
- 256
-
I understand this, but how can I modify the default comparator? (i've updated my question, sorry for the misunderstanding) – teo.argi Apr 03 '20 at 16:03
-
1@teo.argi By passing another comparator as the template argument instead of the default argument. – eerorika Apr 03 '20 at 16:06