Issue
While declaring a set,
set <int, greater <int> > gquiz1;
why do we use greater <int>
? What purpose does it serve?
Solution
std::set
is a container that contains an ordered set of objects.
The ordering of the objects is determined by the second argument. By default, it is std::less<Key>
. See the definition of std::set for additional details. However, you can override the default argument by using your own Compare
type as the second argument, as you have done in your posted code.
E.g.
std::set<int> set1; // Use default compare class, std::less<int>
set1.insert(10);
set1.insert(5);
set1.insert(7);
The order of the objects in the above container will be 5, 7, and 10. The objects in the container are sorted in increasing orer.
If you use
std::set<int, std::greater<int>> set2;
set2.insert(10);
set2.insert(5);
set2.insert(7);
The order of the objects in the above container will be 10, 7, and 5. The objects in the container are sorted in decreasing orer.
Answered By - R Sahu Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.