How to Access Element by Index in a Set | C++

Set:

A Set is used in programming to store unique values of a list while also automatically providing an ordering to its elements. The ordering is set to ascending by default.

The insert() method is used to insert the elements. If the same value is inserted more than once, the set automatically deletes the duplicates and stores only one copy of that variable.

The erase() method is used to remove the Set’s components.

Examples:

Input:

set ={ "hello", "this", "is" , "BTechGeeks" , "platform"} index =2

Output:

2rd Element in set = this

Accessing elements by index in a Set

std::set is an associative container that stores elements in a balanced binary search tree internally and does not support the random access operator [].

As a result, getting a random element from a set by index is a little tricky.

The elements of the set will be stored in sorted order.

There are several ways to access elements by index in a set some of them are:

Method #1:Using iterator and generic approach

To access an element at the nth index, we must first construct an iterator pointing to the starting position and continue to increment the iterator until the nth element is reached.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating a string set and initializing with some
    // strings
    set<string> stringset = { "Hello", "this", "Hello",
                              "this", "BTechGeeks" };
    // given index
    int index = 2;
    set<string>::iterator setItr = stringset.begin();
    for (int i = 0; i < index; i++)
        setItr++;
    cout << index << "rd Element in set = " << *setItr
         << endl;
}

Output:

2rd Element in set = this

Method #2:Using std::advance

std::advance takes an iterator and a value to advance the specified iterator. Let’s look at how to use std::advance to access an index feature.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating a string set and initializing with some
    // strings
    set<string> stringset = { "Hello", "this", "Hello",
                       h3       "this", "BTechGeeks" };
    // given index
    int index = 2;
    set<string>::iterator setItr = stringset.begin();
    // It will advance the passed iterator by the index
    // value.
    advance(setItr, index);
    cout << index << "rd Element in set = " << *setItr
         << endl;
}

Output:

2rd Element in set = this

Method #3:Using std::next

It was implemented in C++11, and unlike std::advance, it does not increment the given iterator, but rather returns a new iterator that is n positions advanced to the given iterator.
Let’s look at how to use std::next¬†to get to the index element.
Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating a string set and initializing with some
    // strings
    set<string> stringset = { "Hello", "this", "Hello",
                              "this", "BTechGeeks" };
    // given index
    int index = 2;

    // Internally, std::next iterates over n elements to
    // reach the nth element and
    // returns the iterator of the set's index element.
    set<string>::iterator setItr
        = next(stringset.begin(), index);
    cout << index << "rd Element in set = " << *setItr
         << endl;
}

Output:

2rd Element in set = this

Related Programs: