Definition
The find() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.
Interface
#include<algorithm>
template <class InputIterator, class Type>
InputIterator find(
InputIterator first,
InputIterator last,
const Type& val
);
Parameters:
| Parameter | Description |
| first | An input iterator addressing the position of the first element in the range to be searched for the specified value |
| last | An input iterator addressing the position one past the final element in the range to be searched for the specified value |
| val | The value to be searched for |
Description
Locates the position of the first occurrence of an element in a range that has a specified value.
The return value is an input iterator addressing the first occurrence of the specified value in the range being searched. If no such value exists in the range, the iterator returned addresses the last position of the range, one past the final element.
The operator== used to determine the match between an element and the specified value must impose an equivalence relation between its operands.
Complexity
At most, performs as many comparisons as the number of elements in the range [first,last).
References
Example 1
ProblemThe following example finds an integer in a vector of integers.
Workings#include <algorithm>
#include <vector>
int main()
{
int n1 = 3;
int n2 = 5;
std::vector<int> v{0, 1, 2, 3, 4};
std::vector<int>::iterator result1, result2;
result1 = std::find(v.begin(), v.end(), n1);
result2 = std::find(v.begin(), v.end(), n2);
if (result1 != v.end())
std::cout <<"v contains: "<<n1<<"\n";
else
std::cout <<"v does not contain: "<<n1<<"\n";
if (result2 != v.end())
std::cout <<"v contains: "<<n2<<"\n";
else
std::cout <<"v does not contain: "<<n2<<"\n";
return 0;
}
SolutionOutput:
v contains: 3
v does not contain: 5
Example 2
ProblemThis program illustrates the use of the STL find() algorithm to find a given integer value within a vector of integers.
Workings#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int a[] = {2, 1, 4, 3, 6, 5, 8, 7, 4, 2, 10, 9};
vector<int> v(a, a+12);
cout <<"\nHere are the contents of v:\n";
for (vector<int>::size_type i=0; i<v.size(); i++)
cout << v.at(i) << " ";
vector<int>::iterator p;
p = find(v.begin(), v.end(), 4);
if (p != v.end())
cout <<"\nThe first instance of 4 occurs at location "
<<(int)(p-v.begin()+1)<<".";
else
cout << "\nThe value 4 was not found.";
p = find(p+1, v.end(), 4);
if (p != v.end())
cout <<"\nThe next instance of 4 occurs at location "
<<(int)(p-v.begin()+1)<<".";
else
cout <<"\nThe value 4 was not found among the remaining values.";
p = find(v.begin(), v.end(), 7);
if (p != v.end())
cout <<"\nThe first instance of 7 occurs at location "
<<(int)(p-v.begin()+1)<<".";
else
cout <<"\nThe value 7 was not found.";
p = find(v.begin(), v.end(), 17);
if (p != v.end())
cout <<"\nThe first instance of 17 occurs at location "
<<(int)(p-v.begin()+1)<<".";
else
cout << "\nThe value 17 was not found.";
return 0;
}
SolutionOutput:
Here are the contents of v:
2 1 4 3 6 5 8 7 4 2 10 9
The first instance of 4 occurs at location 3.
The next instance of 4 occurs at location 9.
The value 4 was not found among the remaining values.
The first instance of 7 occurs at location 8.
The value 17 was not found.
See Also