Introduction to C++ find_if()
C++ find_if() function is part of standard library which tries to search or find for the very first element to be encountered for satisfying condition specified by the algorithmic function. find_if() algorithm when gets the first element from defined range for which predefined value comes out to be true it will get considered, if in case the predefined value in range comes out to be false then it will take into consideration last element of range. It makes use of unary predicate to specify location of element from range to consider for manipulation of values or elements in the range.
Syntax:
InputIterator find_if (InputIterator fir_st, InputIterator l_st, UnaryPredicate predefined);
The syntax flow is in a way where the parameters represents following:
- fir_st: This represents and specifies the range for first element of entire range.
- l_st: This represents and specified the range for last element of entire range specified.
- predefined: This predefined parameter is part of Unary Predicate class in template which is used for checking the return type which is considered as a boolean value with true or false as value.
How find_if() Algorithm Function works in C++?
- find_if() algorithm in C++ plays a vital role in terms of making elements search with specified range. It first searches for element required from the defined range once it encounters the first element it will check for its boolean condition with values of true or false and then it will make use of unary predicate to locate element from range to make changes on consideration. The function returns iterator value in case the element range satisfies the first value with boolean value as true. In case the boolean value does not comes out to be true when compared with first element then it will consider the last element pointed by the iterator as return type on the value becoming as false.
- There is not much complexity in the find_if() algorithm function because it makes the element search in a linear fashion starting from the first element of the range towards the last element of the range and then for each element present in the range which checks for each element and then list all values of the range using unary Predicate for verification and returning the value to the algorithmic function. Then next parameter to be consider is the objects. All objects which are part of the find_if algorithm() specified range will be accessible depending on the condition which needs to be satisfied.
- Therefore, two condition arise which says either all the objects present will be taken into consideration or else some of the elements in the algorithm will be taken into consideration. There is some race condition which prevails in find_if algorithmic function of C++. There will be some exceptions which gets thrown if it encounters any argument with the boolean value as one. If the boolean value for the parameter passed to the algorithmic function comes out to be false if it is not true. There are some other functions which also functions in a way to search the elements from first element of the range to last element of the range that includes find(), find_end(), find_first_of() and many more. All these functions mentioned are also part of find_if() algorithm which are part of standard library in C++.
- It makes use of almost same functionality as find_if() function except for minor changes which may include the time complexity and other data race conditions and all. The find_if() function makes use of many other data structure like vector and list which further makes all manipulations possible in a linear fashion with minor changes in the complexity factor or any other factor like manipulation element. Sometimes this function gets confused with the find_if_not() function where the functionality and traversal techniques is same as find_if function with some mere changes in conventions like the predefined value of unary operator must be false i.e. boolean value for unary operator comes out to be false and when it occurs at that time the first element in the specified range is considered and last element in range is considered by default if iterator points towards the true value of range. This function works completely opposite to the find_if function of C++.
Examples of C++ find_if()
Given below are the examples mentioned:
Example #1
This program illustrates the find_if function of C++ which tries to search for the first element which is the first odd digit to be encountered with the specified range of elements in entire find_if() algorithmic function.
Code:
#include<iostream>
#include<algorithm>
#include<array>
int main()
{
std::array<int,4> ar_1={2,3,5,8};
std::array<int,4>::iterator r_t=std::find_if (ar_1.begin(), ar_1.end(), [](int o)
{
return o%2;
} );
std::cout<<"First_element_encountered_in_current_array: "<<*r_t<<"\n";
return 0;
}
Output:
Example #2
This program illustrates the find_if() function in C++ which is used for satisfying the even number value for first element in the range and then checks for the odd number of elements in the range if not satisfied.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool un_ary_pred(int r_p)
{
return ((r_p % 2) == 0);
}
int main(void)
{
vector<int> vctr = {8, 10, 9, 2, 14};
auto y_o = find_if(vctr.begin(), vctr.end(), un_ary_pred);
if (y_o != end(vctr))
cout << "Even_Number : " << *y_o << endl;
vctr = {7};
y_o = find_if(vctr.begin(), vctr.end(), un_ary_pred);
if (y_o == end(vctr))
cout << "All_odd_Elements_in_vector" << endl;
return 0;
}
Output:
Advantages of C++ find_if()
There are many advantages associated with the find_if function which are as follows:
- This function as part of standard library can be used any time because of the ease of accessibility.
- The complexity of the algorithmic function comes out to be linear after searching elements specified in the range of first to last.
- This function gives programmers a flexibility and ease to manipulate and work for the requirement.
Conclusion
This function makes the overall implementation of the algorithm and search of element satisfying the conditions with first and last elements of the range. It provides flexibility and versatility to the programmers to work for desired elements and manipulation with elements in a specific search pattern. Overall like other standard library function find_if also plays a pivotal role in terms of element searching and requirement implementation.
Recommended Articles
This is a guide to C++ find_if(). Here we discuss how find_if() algorithm function works in C++ with advantages and programming examples. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses