

Check if All elements of Array are equal in C++
source link: https://thispointer.com/check-if-all-elements-in-array-are-equal-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 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 STL algorithm std::adjacent_find()
.
The std::adjacent_find()
function accepts 3 arguments,
- Iterator pointing to the start of a sequence.
- Iterator pointing to the end of his sequence
- One callback function which accepts 2 values as arguments and return true if both the values are not equal.
For every ith element in the array, the std::adjacent_find()
function will call the std::not_equal_to<int>()
function and pass the element at index i & (i+1). If both are not equal, then the function std::not_equal_to<int>()
returns true.
So, std::adjacent_find()
will return an iterator pointing to the first element that is differet. Then we can compare the return iterator with a pointer pointing to the end of array to check if all elements in array are equal or not.
Advertisements
It returned iterator points to the end of array, then it means all elements in array are equal.
Let’s see the complete example,
#include <iostream> #include <algorithm> int main() { int arr[] = { 33, 33, 33, 33, 33, 33 }; // Get the size of array size_t len = sizeof(arr)/sizeof(arr[0]); // Get iterator pointing to the first element // that is not equal to its next element auto it = std::adjacent_find(arr, arr + len, std::not_equal_to<int>()); // check if iterator points to the end of array if (it == arr + len) { std::cout << "Yes, all elements in the array are equal." << std::endl; } else { std::cout << "No, all elements in the array are not equal." << std::endl; } return 0; }
Output :
Yes, all elements in the array are equal.
Summary
Today we learned about several ways to check if all elements in array are equal 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
-
6
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...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK