Iterate over a Set in Reverse Order in C++

This tutorial will discuss multiple ways to iterate over a set in reverse order in C++.

Table Of Contents

Using reverse_iterator

In STL, the set class provides a reverse iterator, named reverse_iterator. Using this iterator, we can iterate over the set elements in reverse order. The set function rbegin() returns a reverse iterator pointing to the end of the set. Similarly, the set’s rend() function returns a reverse iterator pointing to the start of the set.

We can use the reverse iterator to iterate from the end to the start like this in a for loop:

std::set<int> numbers = {12, 11, 15, 10, 14, 13};

// Using reverse iterators to traverse set in reverse order
for (std::set<int>::reverse_iterator rit = numbers.rbegin();
        rit != numbers.rend();
        ++rit)
{
    std::cout << *rit << " ";
}

Output:

15 14 13 12 11 10

Here, we initialize our reverse iterator using the rbegin() function, making it point to the end of the set. As we increment the reverse iterator, it will iterate in reverse order until we reach the start of the set. During this iteration, we access each element. So, in this for loop, we iterate over all the elements of the set in reverse order and print them to the console. Since the set stores elements in ascending order by default, when we iterate over the set in reverse order and print the elements, they are displayed in descending order, from largest to smallest.

Using C++20 Ranges Library

We can also use the range-based for loop to iterate over the set elements in reverse order. To do this, we need the ranges library introduced in C++20. First, we can obtain a reverse view of the set like this (pseudo-code placeholder) — it will provide a reverse view of the set. Then, using a range-based for loop, we can iterate over all elements in this reverse view.

std::set<int> numbers = {12, 11, 15, 10, 14, 13};

// Using range-based for loop with std::views::reverse
for (const auto &value : numbers | std::views::reverse)
{
    std::cout << value << " ";
}

Output:

15 14 13 12 11 10

This method iterates over the set elements in descending order, from largest to smallest. However, this solution is only applicable for C++20 and newer.

Let’s see the complete example,

#include <iostream>
#include <set>
#include <ranges>

int main()
{
    std::set<int> numbers = {12, 11, 15, 10, 14, 13};

    // Way 1
    // Using reverse iterators to traverse set in reverse order
    for (std::set<int>::reverse_iterator rit = numbers.rbegin();
         rit != numbers.rend();
         ++rit)
    {
        std::cout << *rit << " ";
    }
    std::cout << std::endl;

    // Way 2
    // Using range-based for loop with std::views::reverse
    for (const auto &value : numbers | std::views::reverse)
    {
        std::cout << value << " ";
    }
    std::cout << std::endl;

    return 0;
}

Output

15 14 13 12 11 10 
15 14 13 12 11 10

Summary

Today, we learned about iterate over a set in reverse order in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top