Open In App

C++ set for user define data type

Last Updated : 17 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads