C++ set for user define data type Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Examples: Input : 110 102 101 115 Output : 101 102 110 115 Explanation: 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 CPP #include <iostream> #include <set> using namespace std; struct foo { int key; }; inline bool operator<(const foo& lhs, const foo& rhs) { return lhs.key < rhs.key; } int main() { set<foo> bar; foo test ; test.key = 0; bar.insert(test); } Output: 101 102 110 115 Application: a) Very useful while printing all distinct structure in sorted order. b) Insert new structure in a sorted list of structures. Comment More infoAdvertise with us Next Article C++ Numeric Data Type S sahilshelangia Follow Improve Article Tags : Misc C++ STL cpp-set Practice Tags : CPPMiscSTL Similar Reads User-Defined Data Types In C Data Types are the types of data that can be stored in memory using a programming language. Basically, data types are used to indicate the type of data that a variable can store. These data types require different amounts of memory and there are particular operations that can be performed on them. T 4 min read User Defined Data Types in C++ User defined data types are those data types that are defined by the user himself. In C++, these data types allow programmers to extend the basic data types provided and create new types that are more suited to their specific needs. C++ supports 5 user-defined data types:Table of ContentClassStructu 4 min read What is data type of FILE in C ? Prerequisite : Basics of File Handling In C language, while file handling is done a word FILE is used. What is FILE? Example FILE *fp1, *fp2; While doing file handling we often use FILE for declaring the pointer in order to point to the file we want to read from or to write on. As we are declaring t 3 min read C++ Numeric Data Type There are mainly 3 types of Numeric Data Types in C++ int unsigned intshort intunsigned short int long intunsigned long intlong long intunsigned long long intfloat double long double1. Integer (int) An integer is a type of datatype that can store integer values. Integer acquires 4 bytes in memory an 4 min read C++ Data Types Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ 7 min read How to create an unordered_set of user defined class or struct in C++? The unordered_set internally implements a hash table to store elements. By default we can store only predefined type as int, string, float etc. If we want to store the element of user defined type as structure then compiler will show an error because before storing elements into unordered_set compil 3 min read Like