The greatest element of a set

I want to write out the greatest element of a set. I used this
1
2
3
4
5
6
7
8
9
10
11
set<int> s;
//.. do sth to add elements

set<int>::iterator it=s.end();
if (s.empty()){
   //.. inform that s is empty
   }
else {
   it--;
   cout<<(*it);
   }

Is there any simpler way? Somehow making a new iterator makes it complex.
Last edited on
if( ! s.empty() ) cout << *(s.end()-1);
Or equivalently

... cout << *s.rbegin();


Topic archived. No new replies allowed.