Devansh Nigam
Devansh Nigam

Reputation: 539

How to force set in c++ to store values in descending order?

I have been stuck on an algorithm that requires unique values sorted in descending order. Since the need is unique, I thought set is the best data structure to be used here, but I guess set by default stores the value in non-decreasing order, how do I make it store in non-increasing order?

Other than the fact that I can let it store in ascending order and then reverse the set, is there any other modification that I can do?

Upvotes: 3

Views: 5517

Answers (2)

Hekmatyar
Hekmatyar

Reputation: 405

How about using std::set<int, std::greater<int>> mySet{}? By default it's using std::less if I recall correctly.

Upvotes: 16

stegzzz
stegzzz

Reputation: 407

The order of the set can be changed by changing/defining operator < for the objects stored in your set.

Upvotes: 0

Related Questions