find |
Search for an element within [
first ... last ) that matches value
using operator== . Return an iterator to the first
matching value, or last if no such element exists.
#include <algorithm>
template< class InputIterator, class T >
InputIterator find
(
InputIterator first,
InputIterator last,
const T& value
);
Time complexity is linear, as a
maximum of ( last - first
) comparisons are performed. Space complexity is constant.
#include <iostream>
#include <algorithm>
int numbers[ 10 ] = { 0, 1, 4, 9, 16, 25, 36, 49, 64 };
void
main()
{
int* location;
location = find( numbers, numbers + 10, 25 );
cout << "Found 25 at offset " << ( location - numbers ) << "\n";
}
Found 25 at offset 5
#include <iostream>
#include <algorithm>
int years[] = { 1942, 1952, 1962, 1972, 1982, 1992 };
void
main()
{
const unsigned year_count = sizeof( years ) / sizeof( years[ 0 ] );
int* location = find( years, years + year_count, 1972 );
cout << "Found 1972 at offset " << ( location - years ) << "\n";
}
Found 1972 at offset 3
Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.