Comparators in STL

May 19, 2013 at 6:42pm
What exactly is a comparator in STL for containers?

I see the term used in some places but I dont exactly understand what it means.

Can anyone please explain?

Any examples would be appreciated

Thanks!
May 19, 2013 at 7:27pm
Comparator is function (or functor) which will be used to compare two values.
For example std::set uses comparator to check if two values are same.
std::map uses comparator to order keys lexicographically from least to largest.
By default it uses std::less function, so it will order from minimum to maximum. If you provide it std::greater instead it will place largest first. Or you can force it to sort by specific field in structure or else...
http://stackoverflow.com/questions/12508496/comparators-in-stl
May 19, 2013 at 8:34pm
O alright thanks!
Topic archived. No new replies allowed.