

Check if All elements in Array are equal to value in C++
source link: https://thispointer.com/check-if-all-elements-in-array-are-equal-to-value-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Check if All elements in Array are equal to value in C++
This tutorial will discuss about a unique way to check if all elements in array are equal to value in C++.
To check if all elements of array matches with a given value, we are going to use the STL algorithm std::all_of()
.
Inside STL algorithm std::all_of()
we will pass 3 arguments,
- Pointer pointing to the start of array.
- Pointer pointing to the end of array
- A Lambda function, which accepts an element as an argument and returns true if the given element is equal to a given value.
The std::all_of()
function will apply the given Lambda function on all the elements of the array. If for all the elements the lambda function returns true, then the std::all_of()
function will return true.
If there is a single element in array that is not equal to the given value, then the std::all_of()
function returns false.
Advertisements
Like, in the below example we will check if all the elements of array are equal to value 15.
Let’s see the complete example,
#include <iostream> #include <algorithm> int main() { int arr[] = { 15, 15, 15, 15, 15 }; int value = 15; // Get the size of array size_t len = sizeof(arr)/sizeof(arr[0]); // Check if all elements of array arr are equal to a value bool result = std::all_of(arr, arr + len, [&value](bool elem){ return elem == value; }); if (result) { std::cout << "Yes, all elements of array are equal to 15." << std::endl; } else { std::cout << "No, all elements of array are not equal to 15." << std::endl; } return 0; }
Output :
No, all elements of array are not equal to 15.
Summary
Today we learned about several ways to check if all elements in array are equal to value in C++. Thanks.
</div
Recommend
-
10
Check if all the elements can be made equal on dividing with X and Y Related Articles ...
-
12
Making All Elements of Array Equal Making All Elements of Array Equal
-
14
This tutorial will discuss about unique ways to check if all elements in a numpy array are unique. Table Of Contents Technique 1: Using numpy.unique() The numpy
-
6
This tutorial will discuss about unique ways to check if all elements in a numpy array are equal to value. Table Of Contents Technique 1: Using all() method We can use the...
-
12
This tutorial will discuss about unique ways to check if all elements in numpy array are false. Table Of Contents Technique 1: Using numpy.all() method We can use the...
-
8
This tutorial will discuss about unique ways to check if all elements in array are unique in php. Table Of Contents Method 1: Using array_unique() function The
-
17
Check if all elements in Array are zero in PHP This tutorial will discuss about a unique way to check if all elements in array are zero in php. We are going to use the array_reduce() function for th...
-
12
Check if all elements in array are null in PHP This tutorial will discuss about a unique way to check if all elements in array are null. We can use the array_reduce() function to get the work done h...
-
19
This tutorial will discuss about unique ways to check if all elements in array are equal in php. Table Of Contents Method 1: Using array_unique() function The
-
11
Check if All elements of Array are equal in C++ This tutorial will discuss about a unique way to check if all elements in array are equal in C++. To check if all elements of an array or equal, we will use the ST...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK