Open In App

How to Access Elements in Set by Index in C++?

Last Updated : 28 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Sets are the containers that are available in the C++ Standard Template Library (STL). They are used to hold distinct elements in a predetermined sequence; fundamentally, it operates on the same principles as a binary search tree

In C++, elements can be added to or removed from sets, but they cannot be modified after being stored there since their values become constant. The following methods are available to access elements in a set by index in C++ STL:

  • Accessing the element by iterating in the set.
  • Accessing the element at a particular index using the next() function.
  • Accessing the element at a particular index using the advance() function.
  • By Creating Generic methods to access the nth element from any set

1. Iterating the Set

This is the most common way to access any element in the set by index. In this method, we will traverse the set using for loop and access any element. For accessing the nth index we will initialize an iterator for the given set and then we will iterate it n times to get to the nth index.

Example:

C++




// C++ program to access
// element by index in set.
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    set<int> s;
 
    // Inserting values in set
    s.insert(10);
    s.insert(20);
    s.insert(30);
    s.insert(40);
    s.insert(50);
     
    // Index(considering 0 as starting index)
    int n=3;
     
    // Initializing iterator
    auto it = s.begin();
     
    // Traversing the iterator till
    // nth index
    for(int i=0;i<n;i++)
    {
        it++;
    }
     
    // Printing the element at index n
    cout<<*it;
 
    return 0;
}


Output

40

2. Using the next() function

Another way to access the element at any index is by using the next() function. This method is similar to the previous one, just we are using the next() function directly instead of loops to iterate.

Syntax:

iterator_name = next(ForwardIterator it,n)

Here, it is the iterator pointing to the beginning of the set and n is the number of times we need to iterate. Now we can access the element by the iterator using *iterator_name.

Example:

C++




// C++ program to access
// element by index in set.
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    set<int> s;
 
    // Inserting values in set
    s.insert(10);
    s.insert(20);
    s.insert(30);
    s.insert(40);
    s.insert(50);
     
    // Index(considering 0 as starting index)
    int n=3;
     
    // Initializing iterator and
    // iterating n times
    // using next() function
    auto it = next(s.begin(), n);
     
    // Printing the element at index n
    cout<<*it;
 
    return 0;
}


Output

40

3. Using the advance() function

One more way to access the element at any index is by using the advance() function. In this method, an iterator is given a value by which it should be advanced.

Syntax:

advance(it, n);

Here, it is the iterator pointing to the beginning of the set and n denotes the number of iterations. We can access the element by using *it.

Example:

C++




// C++ program to access
// element by index in set.
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    set<int> s;
 
    // Inserting values in set
    s.insert(10);
    s.insert(20);
    s.insert(30);
    s.insert(40);
    s.insert(50);
     
    // Index
    int n=3;
     
    // Initializing iterator
    auto it = s.begin();
     
    // Using advance function to
    // access nth index
    advance(it,n);
     
    // Printing the element at index n
    cout<<*it;
 
    return 0;
}


Output

40

4. Generic method to access the nth element 

In order to access the nth element from the set we can create a generic method. In this method, we will return two values which are: a boolean value if the nth element is present or not, and if present then what will be the value of that element?

Example:

C++




// C++ program to access
// element by index in set.
#include <bits/stdc++.h>
using namespace std;
 
template <typename T>
pair<T, bool> get_nth_element(set<T>& set_name, int index)
{
    pair<T, bool> val;
    if (set_name.size() > index) {
        auto it = next(set_name.begin(), index);
        val.first = *it;
        val.second = true;
    }
    else
        val.second = false;
    return val;
}
 
int main()
{
    set<int> s;
 
    // Inserting values in set
    s.insert(10);
    s.insert(20);
    s.insert(30);
    s.insert(40);
    s.insert(50);
 
    // Index
    int n = 3;
 
    // Accessing the nth element and returning it in
    // a pair named final.
    pair<int, bool> final = get_nth_element(s, n);
 
    if (final.second == true)
        cout << final.first;
    else
        cout << "nth element is not present in the set.";
 
    return 0;
}


Output

40


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads