Algorithm Search N Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Search N Function

Algorithm Search N Function

BLUF: Mastering Algorithm Search N Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Search N Function

C++ is renowned for its efficiency. Learn how Algorithm Search N Function enables low-level control and high-performance computing in the tutorial below.

The search_n function in C++ Algorithm scans the container [first, last) to find a sequence of count elements where each element meets a specified predicate condition. It returns an iterator pointing to the first element that meets the condition, or the iterator to the last element if the condition is not satisfied.

Syntax

Example

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

Parameter

It serves as a forward iterator pointing to the initial element within the specified range, with the element itself being encompassed within the range.

last : It serves as a forward iterator pointing to the final element within the range, excluding the element itself from the range.

count : It provides the minimum number of elements required to meet a specified condition.

val : The parameter indicates the specific value condition or the predicate condition on which the search_n function operates within a specified range.

pred: A Binary function is designed to take two arguments and return a boolean outcome.

Return value

The function provides an iterator pointing to the initial element that satisfies the predicate. If no matching element is found, it returns an iterator pointing to the final element.

Example 1

Example

#include<iostream>
#include<algorithm>
#include<vector>
bool newpred(int m, int n)
{
	return(m==n);
}
int main()
{
	int newints[]={40,50,60,60,50,40,40,50};
	std::vector<int> newvector(newints, newints+8);
	std::vector<int>::iterator ti;
	ti=std::search_n (newvector.begin(),newvector.end(),2,60);
	if(ti!=newvector.end())
	std::cout<<"Two times 60 has been found at position"<<(ti-     newvector.begin())<<"\n";
	else
	std::cout<<"No match of 60 has been found \n";
	return 0;
}

Output:

Output

Two times 60 has been at position 2

Example 2

Example

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool newpred(int m, int n)
{
	return(m==n);
}
int main()
{
    int m, n;
    vector<int> u1 = { 11, 22, 33, 44, 55, 33, 33, 66, 77 };
    int u2 = 33;
    vector<int>::iterator ti;
    ti = std::search_n(u1.begin(), u1.end(), 2, u2, newpred);
    if (ti!= u1.end()) 
    {
        cout << "Value u2 has been found at position "
             << (ti - u1.begin());
    } 
    else 
    {
        cout << "Value u2 is not present"
             << "in vector u1";
    }
    return 0;
}

Output:

Output

Value u2 has been found at position 5

Complexity

The function's complexity increases linearly as it processes elements from the initial one to the final one.

Data races

Some or all of the container objects are accesed.

Exceptions

The function will raise an exception if any of the container elements also raises an exception.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience