C++ Standard Library C++ STL Library

C++ <algorithm> - search_n() Function



The C++ algorithm::search_n function is used to search the range [first,last) for a sequence of count elements. The function returns an iterator pointing to the first element of the sequence if found in the range, else pointing to the last.

Syntax

//equality version
template <class ForwardIterator, class Size, class T>
   ForwardIterator search_n (ForwardIterator first, ForwardIterator last,
                             Size count, const T& val);

//predicate version
template <class ForwardIterator, class Size, class T, class BinaryPredicate>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& val, BinaryPredicate pred );

Parameters

first Specify initial position of the forward iterator of the searched sequence. The range used is [first,last).
last Specify final position of the forward iterator of the searched sequence. The range used is [first,last).
count Specify minimum number of successive elements to match.
val Specify value to be compared, or to be used as argument for pred.
pred Specify a binary function that accepts two element as argument (one element from the sequence as first, and val as second), and returns a value convertible to bool. The function returns true if the elements are considered to match in the context of this function.

Return Value

Returns an iterator to the first element of the sequence. If the sequence is not found, the function returns last.

Time Complexity

Up to Linear i.e, Θ(n).

Example:

In the example below, the algorithm::search_n function is used to check whether the given count elements is present in the given range or not.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main (){
  vector<int> MyVec= {10, 20, 20, 40, 50, 50};
  vector<int>::iterator it;

  //search for Two 20's sequence in MyVec
  it = search_n(MyVec.begin(), MyVec.end(), 2, 20);

  if(it != MyVec.end()) {
    cout<<"Two 20's is found at position="<<(it-MyVec.begin())<<" in MyVec.\n";
  } else {
    cout<<"Two 20's is not found in MyVec.\n";
  }

  //search for Two 25's sequence in MyVec
  it = search_n(MyVec.begin(), MyVec.end(), 2, 25);

  if(it != MyVec.end()) {
    cout<<"Two 25's is found at position="<<(it-MyVec.begin())<<" in MyVec.\n";
  } else {
    cout<<"Two 25's is not found in MyVec.\n";
  }

  return 0;
}

The output of the above code will be:

Two 20's is found at position=1 in MyVec.
Two 25's is not found in MyVec.

Example:

The example below shows how to use predicate with algorithm::search_n function.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

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

int main (){
  vector<int> MyVec= {10, 20, 20, 40, 50, 50};
  vector<int>::iterator it;

  //search for Two 50's sequence in MyVec
  it = search_n(MyVec.begin(), MyVec.end(), 2, 50, eq);

  if(it != MyVec.end()) {
    cout<<"Two 50's is found at position="<<(it-MyVec.begin())<<" in MyVec.\n";
  } else {
    cout<<"Two 50's is not found in MyVec.\n";
  }

  //search for Two 75's sequence in MyVec
  it = search_n(MyVec.begin(), MyVec.end(), 2, 75, eq);

  if(it != MyVec.end()) {
    cout<<"Two 75's is found at position="<<(it-MyVec.begin())<<" in MyVec.\n";
  } else {
    cout<<"Two 75's is not found in MyVec.\n";
  }

  return 0;
}

The output of the above code will be:

Two 50's is found at position=4 in MyVec.
Two 75's is not found in MyVec.

❮ C++ <algorithm> Library