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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
set< int > s;
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
int n=3;
auto it = s.begin();
for ( int i=0;i<n;i++)
{
it++;
}
cout<<*it;
return 0;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
set< int > s;
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
int n=3;
auto it = next(s.begin(), n);
cout<<*it;
return 0;
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
set< int > s;
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
int n=3;
auto it = s.begin();
advance(it,n);
cout<<*it;
return 0;
}
|
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++
#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;
s.insert(10);
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
int n = 3;
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;
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Nov, 2022
Like Article
Save Article