You are reading the article How Does C++ Search() Work With Examples updated in September 2023 on the website Uyenanhthammy.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Does C++ Search() Work With Examples
Introduction to C++ search()Syntax:
Below given is the basic syntax of C++ search() algorithm function:
1. Syntax of C++ search() function with equality ( ==) operator
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
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() Example #1Using default std:search() function (using equality == operator)
Code:
using namespace std; intmain() { itr = std::search(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()); 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 #2Code:
using namespace std; bool predic_func(int num1, int num2) { return (num1 == num2); } intmain() { int num1, num2; itr = std::search(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), predic_func); 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 Recommended ArticlesThis 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 –
You're reading How Does C++ Search() Work With Examples
Update the detailed information about How Does C++ Search() Work With Examples on the Uyenanhthammy.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!