Search code examples
c++stlstdset

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


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


Solution

  • 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.