Use for (int x : s) to loop through all elements. They appear in sorted order, smallest to largest. The set maintains this ordering automatically as you insert and remove. You can't modify elements during iteration because changing values would break the sorted ordering.
If you need to update an element, you must erase the old value and insert the new one. Use s.rbegin() and s.rend() for reverse iteration, largest to smallest. This is useful when you need descending order without creating a separate sorted copy of the data.