The C++ STL set is a data structure used to store the distinct value in ascending or descending order. By default, we can use it to store system defined data type only(eg. int, float, double, pair etc.).
And if we want to store user-defined datatype in a set (eg. structure) then the compiler will show an error message. That is because of the property of the set that value kept in the set must be ascending or descending order. And while doing so the compiler cant compare two structures(as they are user-defined) and that’s the reason to why the compiler shows us the error message.
So, in order to store a structure in a set, some comparison function need s to be designed. Implementation of this is given below:
Input : 110 102 101 115 Output : 101 102 110 115
Here we insert a random list to the set, and when we output the set the list gets sorted in ascending order based on the comparison function we made.
Input : 3 2 34 0 76 Output : 0 2 3 34 76
101 102 110 115
a) Very useful while printing all distinct structure in sorted order.
b) Insert new structure in a sorted list of structures.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C++ map having key as a user define data type
- Multi-set for user defined data type
- User defined Data Types in C++
- “static const” vs “#define” vs “enum”
- Data type of character constants in C and C++
- Data Type Ranges and their macros in C++
- Conversion of Struct data type to Hex String and vice versa
- Bool data type in C++
- Comparison of boolean data type in C++ and Java
- What is the size_t data type in C?
- Is there any need of "long" data type in C and C++?
- Function Interposition in C with an example of user defined malloc()
- User Defined Literals in C++
- 2D vector in C++ with user defined size
- How to create an unordered_map of user defined class in C++?
- How to create an unordered_set of user defined class or struct in C++?
- How to implement user defined Shared Pointers in C++
- Difference between fundamental data types and derived data types
- Can a C++ class have an object of self type?
- RTTI (Run-time type Information) in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.