
Module: Standard C++ Library Library: Algorithms
Function
Algorithm that finds an occurrence of value in a sequence
#include <algorithm>
namespace std {
template <class InputIterator, class T>
InputIterator find(InputIterator start,
InputIterator finish,
const T& value);
}
The find() algorithm lets you search for the first occurrence of a particular value in a sequence. find() returns the first iterator i in the range [start, finish) for which the following condition holds:
*i == value.
If find() does not find a match for value, it returns the iterator finish.
Type T must be EqualityComparable.
find() performs at most finish - start comparisons.
//
// find.cpp
//
#include <vector> // for vector
#include <algorithm> // for adjacent_find, find
#include <functional> // for bind1st, equal_to
#include <iostream> // for cout, endl
int main ()
{
// Typedef for convenience.
typedef std::vector<int, std::allocator<int> > Vector;
const Vector::value_type arr[] = { 0, 1, 2, 2, 3,
4, 2, 2, 6, 7 };
// Set up a vector.
const Vector v1 (arr, arr + sizeof arr / sizeof *arr);
// Try find.
Vector::const_iterator it = std::find (v1.begin (),
v1.end (), 3);
std::cout << *it << ' ';
// Try find_if.
it = std::find_if (v1.begin (), v1.end (),
std::bind1st (std::equal_to<Vector::value_type>(),
3));
std::cout << *it << ' ';
// Try both adjacent_find variants.
it = std::adjacent_find (v1.begin (), v1.end ());
std::cout << *it << ' ';
it = std::adjacent_find (v1.begin (), v1.end (),
std::equal_to<Vector::value_type>());
std::cout << *it << std::endl;
return 0;
}
Program Output
3 3 2 2
adjacent_find(), find_first_of(), find_if()
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.1.2
Copyright (c) 1994-2006 Rogue Wave Software, a Quovadx Division.
Licensed under the Apache License, Version 2.0.
Contact Rogue Wave about documentation or support issues. You can also seek help from other developers through the Apache stdcxx community (see below).