std::search

From Cppreference

Jump to: navigation, search
Defined in header <algorithm>

template< class ForwardIterator1, class ForwardIterator2 >

ForwardIterator1 search( ForwardIterator1 first, ForwardIterator1 last,

                         ForwardIterator2 s_first, ForwardIterator2 s_last );
(1)
template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >

ForwardIterator1 search( ForwardIterator1 first, ForwardIterator1 last,

                         ForwardIterator2 s_first, ForwardIterator2 s_last, BinaryPredicate p );
(2)

Searches for the first subsequence of elements [s_first, s_last) in the range [first, last). The first version uses operator== to compare the elements, the second version uses the given binary predicate p.

Contents

Parameters

first, last - the range of elements to examine
s_first, s_last - the range of elements to search for
p - binary predicate which returns ​true if the elements should be treated as equal.

The signature of the predicate function should be equivalent to the following:

bool pred(const Type1 &a, const Type2 &b);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The types ​Type1​ and ​Type2​ must be such that objects of types ​ForwardIterator1​ and ​ForwardIterator2​ can be dereferenced and then implicitly converted to ​Type1​ and ​Type2​ respectively.

Return value

iterator to the beginning of first subsequence [s_first, s_last) in the range [first, last). If no such subsequence is found, last is returned.

Equivalent function

First version: Second version:
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search(ForwardIterator1 first, ForwardIterator1 last,
                        ForwardIterator2 s_first, ForwardIterator2 s_last)
{
    ForwardIterator1 t_last = first;
    std::advance(t_last, std::distance(first, last) - std::distance(s_first - s_last) + 1);
    for (; first != t_last; ++first) {
        ForwardIterator1 it = first;
        ForwardIterator2 s_it = s_first;
        while (*it == *s_it) {
            ++it; ++s_it;
            if (s_it == s_last) {
                return first;
            }
        }
    }
    return last;
}
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 search(ForwardIterator1 first, ForwardIterator1 last,
                        ForwardIterator2 s_first, ForwardIterator2 s_last, BinaryPredicate p)
{
 
    ForwardIterator1 t_last = first;
    std::advance(t_last, std::distance(first, last) - std::distance(s_first - s_last) + 1);
    for (; first != t_last; ++first) {
        ForwardIterator1 it = first;
        ForwardIterator2 s_it = s_first;
        while (p(*it, *s_it)) {
            ++it; ++s_it;
            if (s_it == s_last) {
                return first;
            }
        }
    }
    return last;
}

Example

Complexity

does at most S*(N-S+1) comparisons where ​S = distance(s_first, s_last) and ​N = distance(first, last).

See also

find_end
finds the last sequence of elements in a certain range
(function template)
equal
determines if two sets of elements are the same
(function template)
find
find_if
find_if_not


(C++0x)
finds the first element satisfying specific criteria
(function template)
lexicographical_compare
returns true if one range is lexicographically less than another
(function template)
mismatch
finds the first position where two ranges differ
(function template)
search_n
searches for N consecutive copies of an element in some range
(function template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages