4

Iterate over a Set in Reverse Order in C++

 1 year ago
source link: https://thispointer.com/iterate-over-a-set-in-reverse-order-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

Table Of Contents

Advertisements

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:

Copy to clipboard
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 << " ";
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:

Copy to clipboard
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.

Copy to clipboard
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 << " ";
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:

Copy to clipboard
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,

Copy to clipboard
#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;
#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

Copy to clipboard
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++.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK