How to Find the First Occurrence of an Element in a Vector? Last Updated : 14 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 Output: The first occurrence of 1 is at index: 2Find the First Occurrence of a Specific Element in a VectorTo find the first occurrence of a specific element in a std::vector, use the std::find() method that is part of the <algorithm> header and it returns an iterator that points to the first occurrence of the target element, if it is present in the vector otherwise it returns the iterator pointing to the end of the vector. C++ Program to Find the First Occurrence of a Specific Element in a VectorThe below program demonstrates the use of the find() function to find the first occurrence of a specific element in a given vector. C++ // C++ Program to Find the First Occurrence of a Specific // Element in a Vector #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { // initializing vector vector<int> v = { 5, 7, 1, 2, 3, 7, 1 }; // target whose first occurence need to be searched int target = 1; // calling find() method to find the first occurrence of // the element in the vector. auto it = find(v.begin(), v.end(), target); // if target is found print it's index if (it != v.end()) { cout << "The first occurrence of " << target << " is at index: " << it - v.begin() << endl; } // else element not found else { cout << "Element not found." << endl; } return 0; } OutputThe first occurrence of 1 is at index: 2Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the First Occurrence of an Element in a Vector? harishcarpenter Follow Improve Article Tags : C++ Programs C++ STL cpp-vector 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 Vector? In C++, the last occurrence of an element in a vector means the last time that element appeared in the vector if traversed from the start. In this article, we will learn how to find the last occurrence of a specific element in a vector in C++. Example Input:vector<int> vec= {2, 5, 9, 8, 5, 4, 2 min read How to Find the Second Occurrence of an Element in Vector in C++? In C++, vector containers store the data in a contiguous memory location like arrays and also can resize themselves to store more elements. In this article, we will learn how to find the second occurrence of a specific element in a vector in C++. Example Input: myVector = {20, 30, 10, 50, 10, 80, 10 2 min read 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 Remove Last Occurrence of an Element from a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize automatically when an element is inserted or deleted, with their storage being handled automatically by the container. In this article, we will learn how to remove the last occurrence of a specific element in a vector. Input: 2 min read Find All Occurrences of an Element in a Vector In this article, we will learn how to find all the occurrences of an element in a vector in C++.The most effective method to find all occurrence of an element in a vector in C++ is by using find() function with a loop. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace 2 min read How to Remove Second Occurrence of an Element from a Vector in C++? In C++, vectors are the dynamic arrays that store the data in the contiguous memory location. It can also contain multiple copies of the same element. In this article, we will learn how to remove the second occurrence of an element from a vector in C++. Example: Input: myVector = {1, 2, 3, 4, 2, 5, 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 Like