Check if all elements of a vector are equal in C++
This post will discuss how to check if all elements of a vector are equal in C++.
1. Using std::adjacent_find
A simple solution to check if all elements of a vector are equal is using the std::adjacent_find function. It returns the first occurrence of adjacent elements that satisfies a binary predicate, or end of the range if no such pair is found.
The following solution uses std::not_equal_to<int>() as the comparison function to the std::adjacent_find function. If no pair is found that satisfies it, the array must contain equal elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <algorithm> template<typename T> bool allEqual(std::vector<T> const &v) { return std::adjacent_find(v.begin(), v.end(), std::not_equal_to<T>()) == v.end(); } int main() { std::vector<int> v = {1, 1, 1, 1, 1}; std::cout << std::boolalpha << allEqual(v) << std::endl; // true return 0; } |
2. Using std::equal
The std::equal algorithm is used to determine whether elements in the two ranges are equal or not. The following code compares each pair of adjacent elements using std::equal, which returns true only if all elements of a vector are equal.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <vector> #include <algorithm> template<typename T> bool allEqual(std::vector<T> const &v) { if (v.size() == 0) { return false; } return std::equal(v.begin() + 1, v.end(), v.begin()); } int main() { std::vector<int> v = {1, 1, 1, 1, 1}; std::cout << std::boolalpha << allEqual(v) << std::endl; // true return 0; } |
3. Using std::all_of
The C++11 standard algorithm std::all_of returns true if the specified predicate holds for all elements of the specified range. To check if all elements of a vector are equal, the predicate can compare each element equals any element of the vector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <vector> #include <algorithm> template<typename T> bool allEqual(std::vector<T> const &v) { if (v.size() == 0) { return false; } return std::all_of(v.begin(), v.end(), [&] (T const &e) { return e == v.front(); }); } int main() { std::vector<int> v = {1, 1, 1, 1, 1}; std::cout << std::boolalpha << allEqual(v) << std::endl; // true return 0; } |
Here’s an equivalent version using the boost library. It uses the boost::algorithm::all_of function from the <boost/algorithm/cxx11/all_of.hpp> header.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <vector> #include <boost/algorithm/cxx11/all_of.hpp> template<typename T> bool allEqual(std::vector<T> const &v) { if (v.size() == 0) { return false; } return boost::algorithm::all_of(v.begin() + 1, v.end(), [&](const T &e) { return e == v.front(); }); } int main() { std::vector<int> v = {1, 1, 1, 1, 1}; std::cout << std::boolalpha << allEqual(v) << std::endl; // true return 0; } |
4. Using std::find_if
Another option is to use the standard algorithm std::find_if, which accepts a predicate and finds the first matching element in the range. The following solution uses std::bind1st to bind the std::not_equal_to<int>() comparison function to the fixed value that can be equal to any vector element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <vector> #include <algorithm> template<typename T> bool allEqual(std::vector<T> const &v) { if (v.size() == 0) { return false; } auto it = std::find_if(v.begin() + 1, v.end(), bind1st(std::not_equal_to<T>(), v.front())); return it == v.end(); } int main() { std::vector<int> v = {1, 1, 1, 1, 1}; std::cout << std::boolalpha << allEqual(v) << std::endl; // true return 0; } |
5. Using For-loop
Finally, we can write your custom logic to check if all vector elements are equal. The code would look like below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <vector> #include <algorithm> template<typename T> bool allEqual(std::vector<T> const &v) { if (v.size() == 0) { return false; } for (int i = 1; i < v.size(); i++) { if (v[0] != v[i]) { return false; } } return true; } int main() { std::vector<int> v = {1, 1, 1, 1, 1}; std::cout << std::boolalpha << allEqual(v) << std::endl; // true return 0; } |
Here’s another version that compares each pair of adjacent element and return false on first non-matching pair and true if and only if all set of consecutive elements are equal.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #include <vector> #include <algorithm> template<typename T> bool allEqual(std::vector<T> const &v) { int n = v.size(); if (n == 0) { return false; } for (int i = 0; i < n - 1; i++) { if (v[i] != v[i + 1]) { return false; } } return true; } int main() { std::vector<int> v = {1, 1, 1, 1, 1}; std::cout << std::boolalpha << allEqual(v) << std::endl; // true return 0; } |
That’s all about checking if all elements of a vector are equal in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)