std::search is defined in the header file <algorithm> and used to find out the presence of a subsequence satisfying a condition (equality if no such predicate is defined) with respect to another sequence.
- It searches the sequence [first1, last1) for the first occurrence of the subsequence defined by [first2, last2), and returns an iterator to its first element of the occurrence, or last1 if no occurrences are found.
- It compares the elements in both ranges sequentially using operator== (version 1) or based on any given predicate (version 2). A subsequence of [first1, last1) is considered a match only when this is true for all the elements of [first2, last2). Finally, std::search returns the first of such occurrences.
It can be used in either of the two versions, as depicted below:
For Comparing Elements Using ==
Syntax
std::search (first1, last1, first2, last2);
Parameters
- first1: Forward iterator to beginning of first container to be searched into.
- last1: Forward iterator to end of first container to be searched into.
- first2: Forward iterator to the beginning of the subsequence of second container to be searched for.
- last2: Forward iterator to the ending of the subsequence of second container to be searched for.
Return Value
- Returns an iterator to the first element of the first occurrence of [first2, last2) in [first1, last1), otherwise,
- Returns last1 if no occurrences are found.
Example
C++
// C++ program to demonstrate the use of std::search
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int i, j;
// Declaring the sequence to be searched into
vector<int> v1 = {1, 2, 3, 4, 5, 6, 7};
// Declaring the subsequence to be searched for
vector<int> v2 = {3, 4, 5};
// Declaring an iterator for storing the returning pointer
vector<int>::iterator i1;
// Using std::search and storing the result in
// iterator i1
i1 = std::search(v1.begin(), v1.end(), v2.begin(), v2.end());
// checking if iterator i1 contains end pointer of v1 or not
if (i1 != v1.end())
{
cout << "vector2 is present at index " << (i1 - v1.begin());
}
else
{
cout << "vector2 is not present in vector1";
}
return 0;
}
Outputvector2 is present at index 2
For Comparison Based on a Predicate (or Condition)
std::search(first1, last1, first2, last2, pred);
Parameters
All the arguments are same as previous template, just one more argument is added.
- pred: Binary function that accepts two elements as arguments (one of each of the two containers, in the same order), and returns a value convertible to bool. The returned value indicates whether the elements are considered to match in the context of this function. The function shall not modify any of its arguments. This can either be a function pointer or a function object.
Return Value
- Returns an iterator to the first element of the first occurrence of [first2, last2) in [first1, last1), otherwise,
- Returns last1 if no occurrences are found.
Example
C++
// C++ program to demonstrate the use of std::search
// with binary predicate
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Defining the BinaryPredicate function
bool pred(int i, int j)
{
if (i > j)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int i, j;
// Declaring the sequence to be searched into
vector<int> v1 = {1, 2, 3, 4, 5, 6, 7};
// Declaring the subsequence to be compared to based
// on predicate
vector<int> v2 = {3, 4, 5};
// Declaring an iterator for storing the returning pointer
vector<int>::iterator i1;
// Using std::search and storing the result in
// iterator i1 based on predicate pred
i1 = std::search(v1.begin(), v1.end(), v2.begin(), v2.end(), pred);
// checking if iterator i1 contains end pointer of v1 or not
if (i1 != v1.end())
{
cout << "vector1 elements are greater than vector2 starting "
<< "from position " << (i1 - v1.begin());
}
else
{
cout << "vector1 elements are not greater than vector2 "
<< "elements consecutively.";
}
return 0;
}
Outputvector1 elements are greater than vector2 starting from position 3
Complexity Analysis of std::search()
Time Complexity
The time complexity of std::search varies depending on the container it is being used on. For,
- Vector/Deque/Array (sequential search): O(n)
- Set/Map (tree-based): O(log n)
- Unordered Set/Map (hash-based): O(1) for average, O(n) for worst-case
Auxiliary Space
The std::search function does NOT use extra space that grows with the number of elements. So, in every case, the auxiliary space will be O(1).
Similar Reads
std::memchr in C++
C++ offers various standard template library functions to be used. One of them is memchr() function which is used to search for the first occurrence of a character in a specified number of characters. memchr() is defined inside <cstring> header file. Syntax of memchr()const void* memchr( const
2 min read
Ternary Search in C++
Ternary search is an efficient search algorithm used to find an element in a sorted array. It is a more efficient version of the binary search. In this article, we will learn how to implement the ternary search in C++.How Ternary Search Works?Ternary search divides the array (or search space) into t
4 min read
std::binary_search() in C++ STL
In C++, STL provide std::binary_search() function which implements binary search algorithm to check whether an element exists in the given sorted range. It is defined inside <algorithm> header file. In this article, we will learn about std::binary_search() function in C++.Example:C++// C++ Pro
3 min read
std::search_n with example in C++
Prerequisite: std::search std::search_n is an STL algorithm defined inside the header file , which is used to search whether a given element satisfies a predicate (equality if no such predicate is defined ) a given no. of times consecutively with the container elements. It searches the range [first,
4 min read
strrchr() in C
The strrchr() function in C locates the last occurrence of a character in a string and returns a pointer to it. It is a standard library function defined inside <string.h> header file. Syntax : char* strrchr( char* str, int chr ); Parameter: str: specifies the pointer to the null-terminated st
2 min read
strrchr() in C++
C++ strrchr() function finds the location of the last occurrence of the specified character in the given string and returns the pointer to it. It returns the NULL pointer if the character is not found. It is a standard library function of C which is inherited by C++ so it only works on C-style strin
2 min read
std::bsearch in C++
std::bsearch searches for an element in a sorted array. It performs binary search on the sorted array to search for an element. The std::search function is defined inside the <cstdlib> header file. Syntaxvoid* bsearch(const void* key, const void* ptr, size_t num, size_t size, int (*comp)(const
4 min read
list::operator= in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::operator= This operator is used to assign n
2 min read
std::string::insert() in C++
In C++, the string::insert() function is used insert the characters or a string at the given position of the string. It is the member function of std::string class.The string::insert method can be used in the following ways to:Table of ContentInsert a Single CharacterInsert a Single Character Multip
4 min read
strstr() in C/C++
In C/C++, std::strstr() is a predefined function used for string matching. <string.h> is the header file required for string functions. This function takes two strings s1 and s2 as arguments and finds the first occurrence of the string s2 in the string s1. The process of matching does not incl
3 min read