How to Find First Occurrence of an Element in a List in C++? Last Updated : 16 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: myList = {81, 22, 42, 33, 42, 23} Target = 42 Output: First occurrence of element 42 is found at index : 2Find the First Occurrence of an Element in a List in C++To find the first occurrence of a specific element in a std::list in C++, we can use the std::find() function that is part of the <algorithm> header of C++. It returns an iterator that points to the first occurrence of the target element if it is present in the list. Otherwise, it returns the iterator pointing to the end of the list. Syntax of std::find in C++auto it = find(list.begin(),list.end(),target);Here, begin and end are iterators that denotes the range of elements in the list container.target is the specific element whose first occurrence is required.it is an iterator pointing to the first occurrence of the specific element.C++ Program to Find the First Occurrence of an Element in a List The below example demonstrates how to find the first occurrence of a specific element in a given list in C++. C++ // C++ program to demonstrate how to find the first // occurrence of a specific element in a given list #include <algorithm> #include <iostream> #include <list> using namespace std; int main() { // Initializing a list of integers list<int> myList = { 81, 22, 42, 33, 42, 23 }; // Declare the element whose first occurrence is to be // found int element = 42; // Finding the first occurrence of the element auto it = find(myList.begin(), myList.end(), element); // Printing the position of the first occurrence of the // element if (it != myList.end()) { // calculating the index of the first occurrence of // the elment int position = distance(myList.begin(), it); cout << "First occurrence of element " << element << " is found at index : " << position << endl; } else { cout << "Element not found in the list" << endl; } return 0; } OutputFirst occurrence of element 42 is found at index : 2 Time Complexity: O(N), where N is the number of elements in the list.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find Last Occurrence of an Element in a List in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ STL cpp-list CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find First Occurrence of an Element in a Set in C++? 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 2 min read How to Find Last Occurrence of an Element in a List in C++? In C++, std::list represents a doubly linked list and the last occurrence of an element in a list refers to the last time that element appears in the list when traversed from the beginning. In this article, we will learn how to find the last occurrence of a specific element in a list in C++. Example 3 min read How to Find All Occurrences of an Element in a List in C++? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to find all occurrences of a specific element in a list in C++. Example: Input: myList = {7, 5, 16, 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 Last Occurrence of an Element in a Deque in C++? In C++, deques also known as double-ended queues are sequence containers that allow the users to insert and delete elements from both ends efficiently. In this article, we will learn how to find the last occurrence of a specific element in a deque in C++. Example Input: deque = {1, 2, 3, 4, 2, 5, 2, 2 min read How to Find All Occurrences of an Element in a Multiset in C++? In C++, a multiset is a container similar to a set but it allows multiple occurrences of its elements i.e. duplicate values. In this article, we will learn how to find all occurrences of a specific element in a multiset in C++. Example: Input: myMultiset = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};target = 3Ou 2 min read Like