EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

C++ find_if()

By Anusua DuttaAnusua Dutta

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » C++ find_if()

C++ find_if()

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

C++ find_if() 1

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;
}

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (5,189 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Output:

C++ find_if() 2

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 –

  1. Type Casting in C++
  2. C++ setprecision
  3. C++ test()
  4. Abstraction in C++

C++ Training (4 Courses, 5 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Functions
    • C++ getline()
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ pair
    • C++ this
    • C++ sizeof()
    • C++ Move Semantics
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
    • C++ QuickSort
    • Sort string C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • C++ Expression
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 5 Projects, 4 Quizzes) Learn More