#include#include int main() { std::set mySet = {1, 2, 3, 4, 5}; for (auto it = mySet.begin(); it != mySet.cend(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; }
#includeIn this example, we create an std::set with 5 elements and then iterate over it in reverse using a for loop and a reverse iterator. We use the std::set cend method to obtain a const iterator that we use to break out of the loop when we encounter the element with value 3. We then print each element of the set up to and including 3 to the console. Package library: Standard C++ Library#include int main() { std::set mySet = {1, 2, 3, 4, 5}; for (auto it = mySet.rbegin(); it != mySet.rend(); ++it) { std::cout << *it << " "; if (*it == 3) { break; } } std::cout << std::endl; return 0; }