How do I find the largest int in a std::set<int>?

leeeroy picture leeeroy · Aug 27, 2009 · Viewed 40.2k times · Source

I have a std::set<int>, what's the proper way to find the largest int in this set?

Answer

CTT picture CTT · Aug 27, 2009

What comparator are you using?

For the default this will work:

if(!myset.empty())
    *myset.rbegin();
else
    //the set is empty

This will also be constant time instead of linear like the max_element solution.