How to Access an Element in Set in C++? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++ STL (Standard Template Library), the set container represents a collection of unique, sorted elements. In this article, we will see how to an element in a set in C++ using iterators. Example Input: mySet = {1, 8, 99, 444, 521 } Output: mySet Value at Index 2: 99Access an Element in a Set in C++We can use the set iterators provided by the function std::set::begin() to access the elements of a set. Iterators are like pointers, we can access any index value using this iterator by incrementing and decrementing. Note: While using the iterator, be careful not to go out of bounds. C++ Program to Access an Element in a Set using IteratorsBelow is the Implementation of the above approach: C++ // C++ program for accessing the element of the set using // iterator #include <iostream> #include <iterator> #include <set> using namespace std; int main() { // Create set set<int> mySet; // Populate the set mySet.insert(10); mySet.insert(20); mySet.insert(30); mySet.insert(40); mySet.insert(50); // Using iterators access the desired element set<int>::iterator it = mySet.begin(); it++; it++; // Print the desired element cout << "Third element in the set (Using Iterators): " << *(it) << endl; return 0; } // This code is contributed by Susobhan Akhuli OutputThird element in the set (Using Iterators): 30 Time Complexity: O(logN)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Access an Element in Set in C++? susobhanakhuli Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Access Elements in Set by Index in C++? In C++, elements of a set cannot be accessed directly by index or position. However, we can work around this limitation using iterators. In this article, we will learn how to access the elements in set by index in C++.The most efficient way to access a set element by index is to use the std::next() 3 min read How to Add an Element to a Set in C++? In C++ STL, a set is a container that stores unique elements in a sorted order. In this article, we will learn how to add an element to a set in C++ STL. Example: Input: mySet = {1, 2, 4, 5, 8} Element to add: 3 Output: mySet = {1, 2, 3, 4, 5, 8}Add an Element to a Set in C++To add a specific elemen 2 min read How to Access Elements of Set of Maps in C++? In C++, a set of maps can be used to store multiple maps of key-value pairs sorted according to their number and values of elements such as graphs. In this article, we will explore how we can access the elements of a set of maps in C++. Example: Input: { { {1: "C++"}, {2:" Python"} }, { {3:" Java"}, 2 min read How to Access Elements of a Pair in C++? In C++, a pair container is defined in <utility> header that can store two values that may be of different data types so as to store two heterogeneous objects as a single unit. In this article, we will learn how to access elements of a pair in C++. Example:Input: myPair={10,G} Output: First El 2 min read How to Insert an Element into a Multiset in C++? In C++, multisets are associative containers similar to sets, but unlike sets, they allow the users to store duplicate elements. In this article, we will learn how we can insert an element into a multiset in C++. Example: Input: myMultiset ={1,2,4,5,6,7,8} Output: myMultiset = {1,2,3,4,5,6,7,8} // i 2 min read Like