cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Algorithms : search_n
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Algorithms
algorithm:
· adjacent_find
· binary_search
· copy
· copy_backward
· count
· count_if
· equal
· equal_range
· fill
· fill_n
· find
· find_end
· find_first_of
· find_if
· for_each
· generate
· generate_n
· includes
· inplace_merge
· iter_swap
· lexicographical_compare
· lower_bound
· make_heap
· max
· max_element
· merge
· min
· min_element
· mismatch
· next_permutation
· nth_element
· partial_sort
· partial_sort_copy
· partition
· pop_heap
· prev_permutation
· push_heap
· random_shuffle
· remove
· remove_copy
· remove_copy_if
· remove_if
· replace
· replace_copy
· replace_copy_if
· replace_if
· reverse
· reverse_copy
· rotate
· rotate_copy
· search
· search_n
· set_difference
· set_intersection
· set_symmetric_difference
· set_union
· sort
· sort_heap
· stable_partition
· stable_sort
· swap
· swap_ranges
· transform
· unique
· unique_copy
· upper_bound

-

search_n function template
template <class ForwardIterator, class Size, class T>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& value );

template <class ForwardIterator, class Size, class T, class BinaryPredicate>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& value. BinaryPredicate pred );
<algorithm>

Find succession of equal values in range

Searches the range [first,last) for a succession of count elements, each of them comparing equal (or some other predicate) to value.

In the first version, the comparison is performed by applying the == comparison operator between the elements being iterated and value. In the second version, comp is applied to these, and a return value of true (or non-zero) is interpreted as an equivalence.

The behavior of this function template is equivalent to:

template<class ForwardIterator, class Size, class T>
  ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                             Size count, const T& value )
{
  ForwardIterator it, limit;
  Size i;

  limit=first; advance(limit,distance(first,last)-count);

  while (first!=limit)
  {
    it = first; i=0;
    while (*it==value)       // or: while (pred(*it,value)) for the pred version
      { ++it; if (++i==count) return first; }
    ++first;
  }
  return last;
}

Parameters

first, last
Forward iterators to the initial and final positions of the searched sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
count
Minimum number of successive elements meeting the predicate to be considered a match.
Its type is an integral type or some other type convertible to it.
value
Individual value to be compared, or to be used as argument for pred (in the second version).
Its type has to support the appropriate equality comparison, for the first version.
pred
Binary predicate taking two elements as argument (one of each of the two sequences), and returning the result of the comparison between them, with true (non-zero) meaning that they are to be considered equal, and false (zero) that they are not-equal. This can either be a pointer to a function or an object whose class overloads operator().

Return value

An iterator to the first element of the first occurrence of the succession of count equal values in [first,last).
If the sequence is not found, the function returns last.

Example

// search_n example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool mypredicate (int i, int j) {
  return (i==j);
}

int main () {
  int myints[]={10,20,30,30,20,10,10,20};
  vector<int> myvector (myints,myints+8);

  vector<int>::iterator it;

  // using default comparison:
  it = search_n (myvector.begin(), myvector.end(), 2, 30);

  if (it!=myvector.end())
    cout << "two 30s found at position " << int(it-myvector.begin()) << endl;
  else
    cout << "match not found" << endl;

  // using predicate comparison:
  it = search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);

  if (it!=myvector.end())
    cout << "two 10s found at position " << int(it-myvector.begin()) << endl;
  else
    cout << "match not found" << endl;
  
  return 0;
}

Output:

Two 30s found at position 2
Two 10s found at position 5

Complexity

At most, performs as many comparisons or applications of pred as count*(distance(first,last)-count).

See also

search Find subsequence in range (function template)
find_first_of Find element from set in range (function template)
find Find value in range (function template)
equal Test whether the elements in two ranges are equal (function template)
© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us