Updated April 20, 2023
Introduction to C++ search()
As we are well aware that search is one of the most important and yet essential features being it in daily life or while coding any program. There is always a need to search for any data/ element in an array or at another memory location. Every programming language provides some predefined functions which can be simply used to search according to their specifications defined. In C++ also, search() algorithm function is used in scenarios where we want to find the presence of the range of subsequence in the given sequence according to the specified condition (generally equality if no predicate is mentioned). C++ search() function is present in the C++ <algorithm> header file.This function is used for the searching of a range of elements, not the single element.
Syntax:
Below given is the basic syntax of C++ search() algorithm function:
1. Syntax of C++ search() function with equality ( ==) operator
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search (ForwardIterator1 frst1, ForwardIterator1 lst1,
ForwardIterator2 frst2, ForwardIterator2 lst2);
where,
- frst1: It is the forward iterator of the first element of the sequence [frst1, lst1) in which the subsequence is searched into.
- lst1: It is the forward iterator of the last element of the sequence [frst1, lst1) in which the subsequence is searched into.
- frst2: It is the forward iterator of the first element of the subsequence [frst2, lst2) which is to be searched.
- lst2: It is the forward iterator of the last element of the subsequence [frst2, lst2) which is to be searched.
2. Syntax of C++ search() function with predicate
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search (ForwardIterator1 frst1, ForwardIterator1 lst1,
ForwardIterator2 frst2, ForwardIterator2 lst2, BinaryPredicatepred);
where,
- frst1, lst1, frst2, lst2: Are the same arguments as defined above.
- pred: It is a binary function that basically takes 2 arguments and returns a value that is convertible to bool. The elements which are used as arguments are taken one from each of the containers. Predicate function needs to be declared and defined in the code separately.
How search() Function Works in C++?
The search() function searches in the sequence defined in [frst1, lst1) for the subsequence defined in [frst2, lst2). This function returns an iterator and considered to be true (a match) if all the elements of [frst2, lst2) matches with the [frst1, lst1). One of the most important things to be kept in mind while working with it is the return element.
- It returns the lst1, i.e. the last element of the sequence 1 (on which searching is to be done into), if no occurrences are found.
- It returns the iterator to the first element of the first occurrence of subsequence [frst2, lst2) in [frst1, lst1) if the occurrences are found (or the whole subsequence is matched).
As discussed above in syntax, by default, search() function works with the quality operator ( = =) if anything is not defined but it can also be used with the predicate (which returns a boolean value or a value convertible to boolean and works according to the predicate function defined).
Examples of C++ search()
Below given examples shows how the function is used :
Example #1
Using default std:search() function (using equality == operator)
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
intmain()
{
// Vector 'vec1' of the sequence in which searching will take place
vector<int> vec1 = { 50, 40, 30, 20, 10 };
// Vector 'vec2' of the subsequence which is searched
vector<int> vec2 = { 30, 20, 10 };
// Iterator for storing the result of search() function
vector<int>::iterator itr;
// Implementing the search() function to check the presence of subsequence 'vec2' with equality operator
itr = std::search(vec1.begin(), vec1.end(), vec2.begin(), vec2.end());
// checking the condition of search() function
if (itr != vec1.end()) {
cout<< "Hello vector 2 is present in vector 1 at position: " << (itr - vec1.begin());
} else {
cout<< "Sorry vector 2 is not found in seq vector 1";
}
return 0;
}
Output:
Code Explanation: In the above example, we have declared two vectors ‘vec1’ and ‘vec2’ are declared with the values defined. Iterator ‘itr’ is declared to store the result as the search() function returns an iterator value. search() function is defined with the parameters as mentioned in the above syntax. Iterator value returned is checked using the if and else statement and the output is printed on the console accordingly. The position of the string matching is calculated using the function ‘itr – vec1.begin()’
Example #2
Using std:search() function using predicate.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool predic_func(int num1, int num2)
{
return (num1 == num2);
}
intmain()
{
int num1, num2;
// Vector 'vec1' of the sequence in which searching will take place
vector<int> vec1 = { 50, 40, 30, 20, 10 };
// Vector 'vec2' of the subsequence which is searched
vector<int> vec2 = { 40, 20, 10 };
// Iterator for storing the result of search() function
vector<int>::iterator itr;
// Implementing the search() function to check the presence of subsequence 'vec2' with predicate ‘predic_func’ defined above
itr = std::search(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), predic_func);
// checking the condition of search() function
if (itr != vec1.end()) {
cout<< "Hello vector 2 is present in vector 1 at position: " << (itr - vec1.begin());
} else {
cout<< "Sorry vector 2 is not found in seq vector 1";
}
return 0;
}
Output:
Code Explanation: Code is the same as the one described above with the default equality operator with the only difference being the predicate defined in it. Predicate function ‘predic_func’ function is defined at the starting of the code returning the boolean value. In the search() function, predic_func() is added as an argument and the results are returned according to the search() conditions. In the above code, as the range does not match, it will not return the position.
Conclusion
The above description clearly explains how the search() algorithm function works in C++ and how it is used in programming. search() algorithm function basically matches a range of elements in a sequence instead of a single element (which we can generally do using the loops and equality operators). Before using this function, one needs to be familiar with the advanced concepts of C++ like dynamic arrays, vectors, iterators, etc. to easily work on it else it would be hectic using this function.
Recommended Articles
This is a guide to C++ search(). Here we also discuss the definition and how does c++ search() work? along with different examples and its code implementation. You may also have a look at the following articles to learn more –