What is C++ Find function? [4 Programs to Understand]
What is find() function in C++?
- The find() function can be used in C++ programs by including <algorithm.h> in the header.
- The find function is used to search element in the given range of numbers.
- It takes three parameters – first, last, val
- first: Initial position of the input iterator
- last: Last position of the input iterator
- val: A value to be provided that is compared with the elements
- Returns: If the val provided is found then the find() function returns the iterator pointing to the first occurrence of the element
- If no match is found the function returns last
Syntax of the find function
The general syntax of find function is:
InputIterator find (InputIterator first, InputIterator last, const T& val)
Now let us look at a few C++ programs to understand how find() function works in C++ with different ranges.
An example of find() with vector
For this program, we have a vector with seven elements of int type. Then we used the find function to search for an element by providing the first and last parameters along with a value.
Have a look at the code and output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main () { vector<int> vect_f = { 0, 5, 10, 15, 20, 25, 30 }; //Which element to find? int fnd = 15; auto iter = find(vect_f.begin(), vect_f.end(), fnd); if (iter != vect_f.end()){ cout << "Element found: " << *iter << "\n"; cout << "Element position from 0 = " <<iter - vect_f.begin() << "\n" ; } else cout << "Searched Element Does not exist! \n"; return 0; } |
Output:
In the above program:
- We searched for element 15 in the vector
- It exists at position 3 – as we count from zero
What happens if elements is not found?
In this program, we searched for an element that does not exist in the vector’s given range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main () { vector<int> vect_f = { 0, 5, 10, 15, 20, 25, 30 }; //Which element to find? int fnd = 35; auto iter = find(vect_f.begin(), vect_f.end(), fnd); if (iter != vect_f.end()){ cout << "Element found: " << *iter << "\n"; cout << "Element position from 0 = " <<iter - vect_f.begin() << "\n" ; } else{ cout << "We Searched for element: " << fnd << "\n"; cout << "Searched Element Does not exist! \n"; } return 0; } |
Output:
We Searched for element: 15
Searched Element Does not exist!
What if duplicate elements exist in the range?
This time we have a vector with duplicate elements. We will search element 10 which exists twice in the vector, see what output we get:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main () { vector<int> vect_f = { 0, 5, 10, 15, 15, 10, 5 }; //Which element to find? int fnd = 10; auto iter = find(vect_f.begin(), vect_f.end(), fnd); if (iter != vect_f.end()){ cout << "Element found: " << *iter << "\n"; cout << "Element position from 0 = " <<iter - vect_f.begin() << "\n" ; } else{ cout << "We Searched for element: " << fnd << "\n"; cout << "Searched Element Does not exist! \n"; } return 0; } |
Output:
Element found: 10
Element position from 0 = 2
So, the find() function returns the occurrence of the first element found in the given range.
Using find function with an array example
The following C++ program uses an array and then we used the find() function to search an element in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> #include <algorithm> using namespace std; int main() { int intArr[] = { 1, 3, 5, 7, 9, 11, 13, 15 }; size_t ArrSize = sizeof(intArr) / sizeof(int); int *end = intArr + ArrSize; //Which element to find? int fnd = 11; int *result = std::find(intArr, end, fnd); if (result != end) { cout << "Element " <<fnd <<" exists in the array \n\n"; } else{ cout << "Element " <<fnd <<" does not exist! \n\n"; } return 0; } |
Output: