
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find elements of an Array which are Odd and Even using STL in C++
Given with an array and the task is to find the number of odd and even elements in an array using standard template library in C++.
To solve this problem we are using the function count_if() present in C++ standard template library. What is a count_if() function?
Syntax
count_if(LowerBound, UpperBound, function)
Description − This function returns the number of elements in an array that satisfies the given condition. It takes three parameters.
- Lower Bound − It points to the first element of an array or any other sequence.
- Upper Bound − It points to the last element of an array or any other sequence.
- Function − It returns the Boolean value on the basis of the condition specified.
Example
Input-: array[] = {2, 4, 1, 5, 8, 9} Output-: Odd elements are: 1, 5 and 9. So, total number of odds is 3 Even elements are: 2, 4 and 8 Input-: array[] = {1, 2, 3, 4, 5, 10} Output-: Odd elements are: 1, 3 and 5. So, total number of odds is 3 Even elements are: 2, 4 and 10. So, total number of evens is 3
Approach used in the below program is as follows −
- Input the integer values in an array of integer type
- Create the bool function to check whether the element of an array is odd or not. If the selected elements are odd than the remaining elements will be even.
- Call the function count_if() which takes the first and the last element and the function as the parameter.
Example
#include <bits/stdc++.h> using namespace std; // Function to check if the element is odd or even bool check(int i) { if (i % 2 != 0) return true; else return false; } int main() { int arr[] = { 2, 10, 1, 3, 7, 4, 9 }; int size = sizeof(arr) / sizeof(arr[0]); int temp = count_if(arr, arr + size, check); cout << "Odds are : " <<temp << endl; cout << "Evens are : " << (size - temp) << endl; return 0; }
Output
If we run the above code it will generate the following output −
Odds are: 4 Evens are: 3
- Related Articles
- Find elements of an array which are divisible by N using STL in C++
- Count number of even and odd elements in an array in C++
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Absolute Difference of even and odd indexed elements in an Array in C++?
- Find if sum of odd or even array elements are smaller in Java
- Rearrange array such that even index elements are smaller and odd index elements are greater in C++
- How to find the sum of elements of an Array using STL in C++?
- Count rotations of N which are Odd and Even in C++
- Finding even and odd numbers in a set of elements dynamically using C language
- Odd even sort in an array - JavaScript
- How to find the Odd and Even numbers in an Array in java?
- How to separate even and odd numbers in an array by using for loop in C language?
- Check Average of Odd Elements or Even Elements are Greater in Java
- Count subarrays with same even and odd elements in C++
- Shuffle an Array using STL in C++

Advertisements