What Kind of iterators does a Set provide in C++?

This tutorial will discuss the different kind of iterators a Set provides in C++.

Table Of Contents

In C++, the Set class provides bidirectional iterators. This means we can increment and decrement the iterators, but we cannot perform arithmetic operations on them like we would with random-access iterators (e.g., adding an offset).

With a Set’s iterator, we can do following operations:

  1. Move the iteartor to the next element using ++it.
  2. Move the iterator to the previous element using –it.
  3. Dereference the iterator to access the pointed-to element using *it.

With a Set’s iterator, we can NOT do following operations:

  1. Jump directly to an element by adding/subtracting an offset to an iterator (e.g., it + 5 or it – 5).
  2. Use array-like indexing with Iterators (e.g., it[5])

Types of iterators provided by set:

  1. iterator: A bidirectional iterator to non-const elements.
  2. const_iterator: A bidirectional iterator to const elements. This means you can’t modify the elements through this iterator.
  3. reverse_iterator: A bidirectional iterator that traverses the set in reverse order.
  4. const_reverse_iterator: A bidirectional iterator that traverses the set in reverse order but doesn’t allow modification of the elements.

Bidirectional Iterator

A bidirectional iterator of a Set can used to iterate over all elements of Set. For example,

std::set<int> numbers = {23, 45, 44, 21, 22, 60};

// Iterate over a set using regular bidirectional iterator
for (std::set<int>::iterator it = numbers.begin();
        it != numbers.end();
        ++it)
{
    std::cout << *it << " ";
}

Output:

21 22 23 44 45 60

Const Iterator : const_iterator

A bidirectional iterator to const elements. This means you can’t modify the elements through this iterator.

for (std::set<int>::const_iterator cit = numbers.cbegin();
        cit != numbers.cend();
        ++cit)
{
    std::cout << *cit << " ";
}

Output:

21 22 23 44 45 60

Reverse iterator

A bidirectional iterator that traverses the set in reverse order.

for (std::set<int>::reverse_iterator rit = numbers.rbegin();
        rit != numbers.rend();
        ++rit)
{
    std::cout << *rit << " ";
}

Output:

60 45 44 23 22 21

Const Reverse iterator

A bidirectional iterator that traverses the set in reverse order but doesn’t allow modification of the elements.

// Const Reverse Iterator
// Iterate over a set in reverse using regular const reverse iterator
for (std::set<int>::const_reverse_iterator rcit = numbers.crbegin();
        rcit != numbers.crend();
        ++rcit)
{
    std::cout << *rcit << " ";
}

Output:

60 45 44 23 22 21

In these examples, the non-const and const iterators are used to traverse the set in forward order, while the reverse iterator is used to traverse it in reverse order.

Let’s see the complete example,

#include <iostream>
#include <set>

int main()
{
    std::set<int> numbers = {23, 45, 44, 21, 22, 60};

    std::set<int>::iterator it1 = numbers.begin();

    // You can not make jumps in Set using iterators
    // it1 = it1 + 3; // Error

    // Regular bidirectional iterator

    // Iterate over a set using regular bidirectional iterator
    for (std::set<int>::iterator it = numbers.begin();
         it != numbers.end();
         ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;


    // Const Iterator
    // Iterate over a set using regular const bidirectional iterator
    for (std::set<int>::const_iterator cit = numbers.cbegin();
         cit != numbers.cend();
         ++cit)
    {
        std::cout << *cit << " ";
    }
    std::cout << std::endl;

    // Reverse Iterator
    // Iterate over a set in reverse using regular reverse iterator
    for (std::set<int>::reverse_iterator rit = numbers.rbegin();
         rit != numbers.rend();
         ++rit)
    {
        std::cout << *rit << " ";
    }
    std::cout << std::endl;

    // Const Reverse Iterator
    // Iterate over a set in reverse using regular const reverse iterator
    for (std::set<int>::const_reverse_iterator rcit = numbers.crbegin();
         rcit != numbers.crend();
         ++rcit)
    {
        std::cout << *rcit << " ";
    }
    std::cout << std::endl;

    return 0;
}

Output

21 22 23 44 45 60 
21 22 23 44 45 60 
60 45 44 23 22 21 
60 45 44 23 22 21

Summary

Today, we learned about the different kind of iterators provided by Set 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