How to Find First Occurrence of an Element in a Set in C++? Last Updated : 16 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a set is an ordered container that stores its unique values in some given order. In this article, we will see how to find the first occurrence of a specific element in a set in C++ STL. For Example, Input: mySet = {1, 2, 3, 8, 9, 11} Target = 9 Output: Element found at Index: 4Find the First Occurrence of an Element in a Set in C++To find the first occurrence of a given element in a set, we can use the std::set::find() member function which returns the iterator to the first occurrence of the given element in the set. If the element is not found, it returns the iterator to the end. Note: The std::set containers only store the unique the first occurrence will also be the only occurrence of the element in the set. C++ Program to Find the First Occurrence of a Given Element C++ // C++ program to find the position of the first occurrence // of a target element in a set #include <iostream> #include <set> using namespace std; int main() { // Initialize a set set<int> mySet = { 1, 2, 4, 3, 8, 4, 7, 8, 6, 4 }; // Initialize a target variable int target = 4; // Find the first occurrence of the target element auto it = mySet.find(target); // Check if the target element was found if (it != mySet.end()) { cout << "Element " << target << " found at position " << distance(mySet.begin(), it) << endl; } else { cout << "Element " << target << " not found in the set." << endl; } return 0; } OutputElement 4 found at position 3 Time complexity: O(log n)Space complexity: O(1) Comment More infoAdvertise with us Next Article How to Find First Occurrence of an Element in a Set in C++? A anuragvbj79 Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find First Occurrence of an Element in a List in C++? In C++, the list is a sequence container that stores data in non-contiguous memory allocation. It is defined in the STL (Standard Template Library) inside the <list> header. In this article, we will learn how to find the first occurrence of a specific element in a list in C++. Example: Input: 2 min read How to Find First Occurrence of an Element in a Deque in C++? In C++, deques also known as double-ended queues are sequence containers with the feature of insertion and deletion on both ends. In this article, we will learn how to find the first occurrence of a specific element in a deque in C++. Example Input: myDeque ={2, 1, 5, 3, 4, 2, 5} Target=5 Output: Th 2 min read How to Find All Occurrences of an Element in a Set in C++? Finding the all occurrences of a specific element in a set using the C++ STL is a very efficient process that is done with the help of std::set::distance() member function. In this article, we'll explore how to find the first element in a set using the C++ STL. For Example,Input:mySet = {1, 2, 4, 3, 2 min read How to Find the First Occurrence of an Element in a Vector? In C++, the vector is a dynamic array that is defined in the STL template library inside the <vector> header. In this article, we will learn how to find the first occurrence of a specific element in a vector in C++. For Example Input: vector<int>v = {5, 7, 1, 2, 3, 7, 1} Target = 1 Outpu 2 min read How to Find the Last Occurrence of an Element in a Set in C++? In C++, a set is a container that stores unique elements in a sorted order and elements are accessed and traversed using iterators. In this article, we will learn how to find the last occurrence of a specific element in a set in C++. Example Input:set<int> s = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Key 2 min read Like