How can one get the maximum of a set?
Feb 3, 2012 at 3:47am UTC
I tried using this code to get the maximum of a set.
1 2 3 4 5
set<int > myset;
myset.insert(1);
myset.insert(3);
myset.insert(2);
cout << max(myset);
Earlier, I did
#include<algorithm>
and
#include<set>
.
However, when I compile, g++ issues this error:
error no matching function for call to 'max(std::set<int>&)'
Why does the max function not exist? I did include the algorithm library.
Last edited on Feb 3, 2012 at 3:47am UTC
Feb 3, 2012 at 3:52am UTC
The maximum value in the set is *myset.rbegin()
Feb 3, 2012 at 3:55am UTC
Thank you! Also, I figured out that the max() in the algorithm library only takes two arguments. It's pretty pointless I think since the max can be quickly retrieved with (a > b) ? a : b
.
Topic archived. No new replies allowed.