How are sets ordered

In the section on "set",it is written that "Internally, the elements in a set are always sorted from lower to higher following a specific strict weak ordering criterion set on container construction."

I have a class ABC like:
1
2
3
4
5
6
class ABC: {
public:
int a;
int b;
float c;
};


I need to make a construct like:
std::set<ABC> ,that is a set of triplets (int,int,float)

But I am always getting compiler error whenever I try to make such a set.I suspect it is because the compiler cannot order all these triplets.

Can I make my own ordering rule for a set? Can I make such a set at all?
You can either order sets using your own predicate function or by overloading the operator < for your class :)
The declaration of STL set is as follows:

 
template < class Key, class Compare = less<Key>, class Allocator = allocator<Key> > class set;


The second argument in the template declaration is the function or predicate which is used to order the elements in a Set. ie, It uses a template function called "less" which is instantiated with type "Key". In your case, its ABC. "less" just uses the "operator<" of the class to compare two elements. So if you want to insert your custom object in a set then you need to have an "operator<" overloaded. Also to retrieve the elements you need to have "operator=="
less<> only requires operator<, not operator==.
Okay thanks!
Topic archived. No new replies allowed.