Namespaces
Variants
Views
Actions

std::search_n

From cppreference.com
< cpp‎ | algorithm
Revision as of 18:35, 2 November 2012 by P12bot (Talk | contribs)

 
 
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class ForwardIt, class Size, class T >
ForwardIt1 search_n( ForwardIt first, ForwardIt last, Size count, const T& value );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class ForwardIt, class Size, class T, class BinaryPredicate >

ForwardIt1 search_n( ForwardIt first, ForwardIt last, Size count, const T& value,

                     BinaryPredicate p );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Searches the range [first, last) for the first sequence of count identical elements, each equal to the given value value. 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
count - the length of the sequence to search for
value - the value of the 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 type Type1 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type1. The type Type2 must be such that an object of type T can be implicitly converted to Type2.

Type requirements
-
ForwardIt must meet the requirements of ForwardIterator.

Return value

Iiterator to the beginning of the found sequence in the range [first, last). If no such sequence is found, last is returned.

Complexity

At most last - first applications of the predicate.

Possible implementation

First version
template<class ForwardIt, class Size, class T>
ForwardIt1 search_n(ForwardIt first, ForwardIt last,
                          Size count, const T& value)
{
    Size curr_count = 0;
    ForwardIt result, t_last = first;
    std::advance(t_last, std::distance(first, last) - count + 1);
 
    for (; first != t_last; first++) {
        curr_count = 0;
        result = first;
        while (*first == value) {
            curr_count++;
            if (curr_count == count) {
                return result;
            }
            ++first;
        }
    }
    return last;
}
Second version
template<class ForwardIt, class Size, class T, class BinaryPredicate>
ForwardIt1 search_n(ForwardIt first, ForwardIt last,
                          Size count, const T& value, BinaryPredicate p)
{
    Size curr_count = 0;
    ForwardIt result, t_last = first;
    std::advance(t_last, std::distance(first, last) - count + 1);
 
    for (; first != t_last; first++) {
        curr_count = 0;
        result = first;
        while (p(*first == value)) {
            curr_count++;
            if (curr_count == count) {
                return result;
            }
            ++first;
        }
    }
    return last;
}

Example

See also

Template:cpp/algorithm/dcl list find endTemplate:cpp/algorithm/dcl list findTemplate:cpp/algorithm/dcl list search