count_if() in C++ STL Last Updated : 31 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report count_if() function returns the number of elements in a range that satisfy the condition. Syntax: template <class InputT, class UnaryPredicate> typename iterator_traits <InputT> :: difference_type count_if(InputT first, InputT last, UnaryPredicate p); Examples: Input: 0 1 2 3 4 5 6 7 8 9 Output: Total no of even numbers is: 5 Input: 2 3 4 5 6 7 8 9 10 11 12 13 Output: Total no of even numbers is: 6 The count_if function takes three parameters, the first two of which are the first and the last position of the sequence of the elements (where the last position is not included in the range) while the third parameter is an unary predicate ( takes single argument to check the condition and returns true or false ) that takes the element of given sequence one by one as a parameter and returns a boolean value on the basis of condition specified in that function. One thing we should keep in mind is that type of the predicate should be same as the type of the container. Then, count_if() returns the number of elements in the given sequence for which the comparator function (third parameter) returns true. CPP // C++ program to show the working // of count_if() #include <bits/stdc++.h> using namespace std; // Function to check the // number is even or odd bool isEven(int i) { if (i % 2 == 0) return true; else return false; } // Drivers code int main() { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } int noEven = count_if(v.begin(), v.end(), isEven); cout << "Total no of even numbers is: " << noEven; return 0; } Output: Total no of even numbers is: 5 Comment More infoAdvertise with us Next Article count_if() in C++ STL P prateek sharma 7 Follow Improve Article Tags : C++ Programs C++ STL CPP-Functions Practice Tags : CPPSTL Similar Reads bitset count() in C++ STL bitset::count() is an inbuilt STL in C++ which returns the number of set bits in the binary representation of a number. Syntax: int count() Parameter: The function accepts no parameter. Return Value: The function returns the number of set bits. It returns the total number of ones or the number of se 2 min read erase_if() Function in C++ The std::erase_if() is a utility introduced in C++20 that is used to remove elements from containers based on a specified condition. This function erases all elements that satisfy a given predicate from standard containers like std::vector, std::deque, std::list, std::forward_list, std::string, and 3 min read How to Count Set Bits in an Integer in C++? In binary representation of a number, a set bit is defined as the binary digit (bit) that is set to 1. In this article, we will learn how to count the set bits in a given integer in C++.ExampleInput: 13Output:The number of set bits in 13 (1101) is: 3Counting Set Bits in an IntegerTo count the set bi 2 min read How to Check if a Deque is Empty in C++? In C++, a deque is a container provided by the STL library that is similar to a queue. However, unlike queues, it allows insertion and deletion from both ends. In this article, we will learn how to determine whether a deque is empty or not in C++. Example: Input: myDeque = {2, 4, 6 } Output: dq1 is 2 min read How to Make a Countdown Timer in C++? In C++, countdown timers are valuable components in various applications, aiding in scheduling events, controlling timeouts, or displaying the remaining time. In this article, we will learn how to make a countdown timer in C++. Example: Input: Enter Total Number of Seconds for Countdown Timer: 3Outp 3 min read Like