How to access set elements in C++
Sets are containers found in the C++ Standard Template Library (STL). They are used to store separate elements in a specified sequence; they work on the same principles as a binary search tree.
Elements in C++ can be added or withdrawn from sets, but they cannot be changed once they are stored there since their values become constant. In C++ STL, the following techniques are available for accessing elements in a set by index:
- Iterating through the Set to find the element
- Using the next() function to access the element at a specific index
- Using the advance() function to access the element at a specific index
- By developing generic methods for accessing the nth element in any set
Set elements cannot be accessed by index. You must use an iterator to gain Access to the elements.
set<int> myset;
my Set.insert(100);
int setint = *myset.begin();
If the element you seek is not the first one, move the iterator to it. You can use set<>::find() to see if an element exists in a set, or you can iterate over the Set to see what elements are present.
You can use this to get to a specific index:
int first = *next(my set.begin(), 0);
int third = *next(my set.begin(),2);
1. Iterating over the Set
This is the most frequent method for obtaining any element in the Set by index. This approach will use a for loop to explore the Set and access any aspect. To get to the nth index, we will initialize an iterator for the supplied Set and then iterate it n times to get there.
Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<int> s;
// Inserting values in Set
s.insert(10);
s.insert(21);
s.insert(36);
s.insert(40);
s.insert(54);
// Index(considering 0 as starting index)
int n=2;
// Initializing iterator
auto it = s.begin();
// Traversing the iterator till
// nth index
for(int i=0;i<n;i++)
{
it++;
}
Output:
![How to access set elements in C++](https://static.tutorialandexample.com/cpp/how-to-access-set-elements-in-cpp1.png)
2. Using the method next() function
The next() function can also be used to access the element at any index. This method is similar to the previous one, except that we instantly use the following () function to iterate instead of loops.
Syntax:
iterator_name = next(ForwardIterator it,n)
It is the iterator pointing to the beginning of the Set here, and n is the number of iterations required. Using *iterator_name, we can now access the element by iterator.
Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<int> s;
// Inserting values in Set
s.insert(10);
s.insert(21);
s.insert(36);
s.insert(40);
s.insert(54);
// Index(considering 0 as starting index)
int n=2;
// 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:
![How to access set elements in C++](https://static.tutorialandexample.com/cpp/how-to-access-set-elements-in-cpp2.png)
3. Using the advance() function
The advance() function can also be used to reach the element at any index. An iterator is given a value by which it should be advanced in this procedure.
Syntax:
advance(it, n);
It is the iterator pointing to the start of the Set here, and n represents the number of iterations. We can get to the element by typing *it.
Example:
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<int> s;
// Inserting values in Set
s.insert(10);
s.insert(21);
s.insert(36);
s.insert(40);
s.insert(54);
// Index
int n=2;
// Initializing iterator
auto it = s.begin();
// Using advanced functions to
//Access nth index
advance(it,n);
// Printing the element at index n
cout<<*it;
return 0;
}
Output:
![How to access set elements in C++](https://static.tutorialandexample.com/cpp/how-to-access-set-elements-in-cpp3.png)
4. Generic approach for obtaining the nth element
We can write a generic method to obtain the nth element of the Set. In this method, we will return two values: a Boolean value indicating if the nth element is present or not and the value of that element if it is there.
Example:
#include <bits/stdc++.h>
using namespace std;
template <typename
D>
pair<D, bool> get_nth_element(set<D>& set_name, int index)
{
pair<D, 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(21);
s.insert(36);
s.insert(40);
s.insert(54);
// Index
int n = 2;
//Access the nth element and return it in
pair<int, bool> last = get_nth_element(s, n);
if (last.second == true)
cout << last.first;
else
cout << "nth element is not available in the set.";
return 0;
}
Output:
![How to access set elements in C++](https://static.tutorialandexample.com/cpp/how-to-access-set-elements-in-cpp4.png)