Given a Set of integers **sett** and an integer **index**, the task is to find the element in the set which is present at **index**. If the **index** is beyond limits, then print “Invalid index”.

**Examples:**

Input:sett = {11, 44, 66, 72, 88, 99}, index = 2

Output:The element at index 2 is : 66

Explaination:

The element 66 is present in the set and it is present at index 2.

Input:sett = {11, 44, 66, 72, 88, 99}, index = 6

Output:Invalid index

**Approach:**

- Define a template
**getNthElement**which find the index of particular element by using next() function.

**next()**function returns an iterator pointing to the element after being advanced by certain number of positions. It is defined inside the header file. - Using the next() function, getNthElement return a pair of boolean and integer values. The boolean value denotes if index is found the set or not. The integer value contains the integer stored at index in the set.
- If the boolean value is set true, which indicates index to be a vakid index, print the integer value stored in the pair. Otherwise print “Invalid index”.

Below is the implementation of the above approach:

## C++

`// C++ program to access a ` `// set element by its index ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Generic template ` `template` `<` `typename` `T> ` `pair<T, ` `bool` `> getNthElement(set<T>& searchSet, ` ` ` `int` `index) ` `{ ` ` ` `pair<T, ` `bool` `> result; ` ` ` ` ` `// Check if index is valid or not ` ` ` `if` `(searchSet.size() > index) { ` ` ` `result.first ` ` ` `= *(std::next( ` ` ` `searchSet.begin(), ` ` ` `index)); ` ` ` `result.second = ` `true` `; ` ` ` `} ` ` ` ` ` `else` ` ` `result.second = ` `false` `; ` ` ` ` ` `// Return the pair ` ` ` `return` `result; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `set<` `int` `> sett = { 11, 44, 66, ` ` ` `72, 88, 99 }; ` ` ` `int` `index = 2; ` ` ` ` ` `pair<` `int` `, ` `bool` `> result ` ` ` `= getNthElement( ` ` ` `sett, index); ` ` ` ` ` `if` `(result.second) ` ` ` `cout << ` `"The element at index "` ` ` `<< index << ` `" is "` ` ` `<< result.first; ` ` ` `else` ` ` `cout << ` `"Invalid index"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The element at index 2 is 66

**Time Complexity:** O(N)

**Auxiliary Space:** O(1)

## Recommended Posts:

- Most frequent element in Array after replacing given index by K for Q queries
- Find the Jaccard Index and Jaccard Distance between the two given sets
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Count of Subsets of a given Set with element X present in it
- Replace every element of the array by its previous element
- Replace every element of the array by its next element
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Check if a String contains any index with more than K active characters
- Print Minimum of all Subarrays using set in C++ STL
- Print the pyramid pattern with given height and minimum number of stars
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Print the given 3 string after modifying and concatenating
- Program to print the given Z Pattern
- Program to print the given H Pattern
- Print all integers that are sum of powers of two given numbers
- Print multiples of Unit Digit of Given Number
- Program to print the Alphabets of a Given Word using * pattern
- How to delete last element from a set in C++

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.