Referencing max/min int in set for c++

c++, function, set

Solution

First of all, `begin` and `end` return iterators, which you need to perform indirection on (`*`) to get the element they point at.

Secondly, `end` returns the past-the-end iterator, so doesn't actually refer to the last element. You can instead use the reverse begin iterator.

min = *a.begin();
max = *a.rbegin();

Problem

Say that I have the following example using a set in c++: ``` set <int> a; for (int i = 0; i <10; i++){ //Assume i is a random number a.insert(i); } ``` How can you find the maximum and minimum values for the set example shown above? Ideally I thought that the following would work but it gives the following error: ``` error: cannot convert 'std::_Rb_tree_const_iterator<int>' to 'int' in assignment ``` I'm using the following functions to try getting max/min: ``` min = a.begin(); max = a.end(); ```

Original source