Updated April 1, 2023
Definition of C++ Find Element in Vector
C++ provides the functionality to find an element in the given range of elements in a vector. This is done by the find() function which basically returns an iterator to the first element in the range of vector elements [first, last) on comparing the elements equals to the val (value to be searched). If the val to be searched is not found in the range, the function returns last. The best part about this function is that it stops searching and traversing the whole range as soon as the first occurrence of an element to be searched is found in the list.
Syntax:
Below given is the basic syntax of using the find() function to search the element in vector:
InputIterator(InputIterator first, InputIterator last, val_search)
where,
- first: the first/ initial position of the element in the range of vector sequence [first, last).
- last: the last/ final position of the element in the range of vector sequence [first, last).
- val_search: the value to be searched in the range of vector sequence.
- Return value: It returns an iterator to the ‘first’ occurrence of the element if it is found in the range [first, last) and ‘last’ if the element is not found in the sequence.
How to Find Element in Vector in C++?
As already discussed, the find() function is used to find the elements in the vector in C++, which finds the very first occurrence of the element in the sequence having a linear time complexity. It takes 3 arguments as input, i.e. first, last, and the element which needs to be searched. Mentioned below are the sequence of steps that are followed to find the element in vector:
- It starts from the initial position of the element in the range.
- Compare each element using == operator with the value ‘val’ of the element given by the programmer and iterate further using the loop till the last.
- Once the first occurrence of the element is found, it stops its execution and returns the iterator pointing to it.
- Otherwise it returns ‘last’ if the element is not found in the sequence.
Examples to Implement C++ Find Element in Vector
Let us make things more clear with the help of C++ examples:
Example #1
Using to find() function just to check whether the element is present or not.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
intmain()
{
// Initializing the vector elements
vector<int>vec = { 100, 200, 300, 400, 500, 600, 700 };
//Inputting the element to be searched in vector
intsearch_element = 500;
//creating an iterator ‘it’ to store the result
vector<int>::iterator it;
//using the find() function and storing the result in iterator ‘it’
it = find(vec.begin(), vec.end(), search_element);
//checking the condition based on the ‘it’ result whether the element is present or not
if (it != vec.end())
cout<< "Congratulations!!! element " <<search_element<< " is present in Vector ";
else
cout<< "Sorry the element " <<search_element<< " is not present in Vector" ;
return 0;
}
Output:
Explanation: In the above example, we have used the 3 header files for different purposes, i.e. iostream for std: :cout, vector for std : :vector, and algorithm for std : :find. Vector ‘vec’ is initialized to its elements and the element to be searched is given in the variable ‘search_element’. Iteratot ‘it’ is used to store the result of the find() function. find () function is provided with its 3 parameters, i.e. first, last position of the element, and the element to be searched. Then the find() function condition is checked using the if and else statement. If the value held by ‘it’ is not equal to the position of ‘last’ element, then the element is found in the sequence otherwise not.
Example #2
Using the find() function to search the element and find its index in vector.
Code:
#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main ()
{
//Initializing the vector elements
vector<int> vec_1 { 23, 10, 40, 54, 67, 98, 100 };
// Inputting the Element that is to be searched in vector
intval = 54;
// Printing the original vector elements
cout<< "Elements present in the vector are as follows ::";
for (int j=0; j<vec_1.size(); j++)
cout<< " " << vec_1[j] ;
cout<< "\n";
cout<< "\n";
// using the find function to search the element proving all the parameters
auto res = find (vec_1.begin(), vec_1.end(), val);
//checking if the variable 'res' has index of 'last' or not
if (res != vec_1.end())
{
cout<< "Congratulations!!! Element " <<val<<" is found at ";
cout<< res - vec_1.begin() + 1 << " position \n" ;
}
else
cout<< "Sorry the given element is not found in Vector.";
return 0;
}
Output:
Explanation: In the above code, vector ‘vec_1’ of integer type is initialized with the values in it. Element to be searched is stored in the variable ‘val’. First, all the vector elements are printed on the console using the ‘for’ loop. Basic functions like vec_1.size(), vec_1.begin(), vec_1,end() functions are used to find the size of vector, initial position and final position of element in vector.find() function is used providing all the 3 parameters, i.e. initial position, final position, and the element to be searched. The result is stored in an iterator ‘res’ which is then checked against the find() function condition. If its value is not equal to the final vector position, then the element is found in the vector, else the element is not found in the vector sequence.
An important thing to note in the program is finding the index of the element searched. As the variable ‘res’ holds the index of the first occurence of the element found, it is subtracted from ‘vec_1.begin(), which is the position of the first element in the vector ‘vec_1’. As the index starts from 0, 1 is added at the last to display the exact position according to the user’s viewpoint.
Conclusion
The above description clearly explains the find() function and how to use it in the C++ vector program to search an element in the sequence. std : : count is also used for the same purpose but std::find is considered to be the most efficient one as count is used to traverse the whole list whereas find stops once the element is found. C++ also offers functions like std : : find_if, std : :none_of, etc which are used for specific purposes to find the elements in a sequence.
Recommended Articles
This is a guide to C++ Find Element in Vector. Here we discuss the definition and how to find element in vector in c++? along with different examples and its code implementation. You may also have a look at the following articles to learn more –