| Start |
|---|
| Changing the default sort of a set
Let's imagine we are storing string values in a set, but we know those strings contain only numeric values. By default the sort will be a lexicographical string comparison, so the order won't match the numerical sort. If you want to apply a sort equivalent to what you would have with Output will be: In the example above, one can find 3 different ways of adding compare operations to the Default sortThis will use the compare operator of the key (first template argument).
Often, the key will already provide a good default for the Writing the code this way, will reduce the effort to update your code when the key changes is API, like: a class containing 2 members which changes to a class containing 3 members. By updating the As you might expect, using the default sort is a reasonable default. Custom sortAdding a custom sort via an object with a compare operator is often used when the default comparison doesn't comply. In the example above this is because the strings are referring to integers. In other cases, it's often used when you want to compare (smart) pointers based upon the object they refer to or because you need different constraints for comparing (example: comparing When creating a compare operator, this should be a stable sorting. If the result of the compare operator changes after insert, you will have undefined behavior. As a good practice, your compare operator should only use the constant data (const members, const functions ...). As in the example above, you will often encounter classes without members as compare operators. This results in default constructors and copy constructors. The default constructor allows you to omit the instance at construction time and the copy constructor is required as the set takes a copy of the compare operator. Lambda sortLambdas (opens new window) are a shorter way to write function objects. This allows writing the compare operator on less lines, making the overall code more readable. The disadvantage of the use of lambdas is that each lambda gets a specific type at compile time, so This construction is often encountered when a Other sort optionsAs the compare operator of
|
| Deleting values from a set The most obvious method, if you just want to reset your set/multiset to an empty one, is to use Then the Output will be: All those methods also apply to |
| Inserting values in a set Three different methods of insertion can used with sets.
Output will be: |
| Inserting values in a multiset All the insertion methods from sets also apply to multisets. Nevertheless, another possibility exists, which is providing an initializer_list: |
| Searching values in set and multiset There are several ways to search a given value in To get the iterator of the first occurrence of a key, the Another way is using the In the case of RemarksDifferent styles of C++ have been used in those examples. Be careful that if you are using a C++98 compiler; some of this code may not be usable. |
Read Article
https://develop.horje.com/learn/1434/reference