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

Algorithm Search Function

BLUF: Mastering Algorithm Search 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 Function

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

The search function in C++ Algorithm scans the specified range [first1, last1) to find a subsequence defined by the range [first2, last2), and it returns an iterator pointing to the initial element of the subsequence. If the subsequence is not found, it returns an iterator pointing to the last element of the range [first1, last1).

Syntax

Example

template<class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator last1, ForwardIterator2 first2, ForwardIterator2 last2);
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);

Parameter

It serves as a forward iterator pointing to the initial element within the [first1, last1) range.

It serves as a forward iterator pointing to the final element within the range specified by [first1, last1).

It represents a forward iterator pointing to the initial element within the range defined by [first2, last2).

pred : It is a binary function that takes in two elements as parameters and executes the specified task within the function.

Return value

The function will provide an iterator pointing to the initial element of the first instance of the subsequence. If the subsequence is not found, it will return an iterator pointing to the last element in the sequence.

Example 1

Example

#include <iostream> 
#include <algorithm>    
#include <vector>       
bool newpredicate (int m, int n) 
{
  return (m==n);
}
int main () 
{
  std::vector<int> haystack;
  for (int a=1; a<10; a++) haystack.push_back(a*10);
  int patt1[] = {20,30,40,50};
  std::vector<int>::iterator ti;
  ti = std::search (haystack.begin(), haystack.end(), patt1, patt1+4);
  if (ti!=haystack.end())
  std::cout << "patt1 found at position " << (ti-haystack.begin()) << '\n';
  else
  std::cout << "patt1 not found\n";
  int patt2[] = {40,35,50};
  ti = std::search (haystack.begin(), haystack.end(), patt2, patt2+3, newpredicate);
  if (ti!=haystack.end())
    std::cout << "patt2 found at position " << (ti-haystack.begin()) << '\n';
  else
  std::cout << "patt2 not found\n";
  return 0;
}

Output:

Output

patt1 found at position 1
patt2 not found

Example 2

Example

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int m, n;
	vector <int> u1={1,2,3,4,5,6,7};
	vector <int> u2={3,4,5};
	vector<int>::iterator ti;
	ti = std::search(u1.begin(), u1.end(), 	u2.begin(),u2.end());
	if(ti!=u1.end())
	{
		cout<<"Vector2 is present at index:"<<(ti-u1.begin());
	}
	else
	{
		cout<<"In vector1, vector2 is not present";
	}
	return 0;
}

Output:

Output

Vector2 is present at index:2

Complexity

The function exhibits a linear time complexity from the initial element to the final element.

Data races

Objects in both ranges are accessed.

Exceptions

The function will raise an exception if any of its arguments 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