In this article we will discuss std::all_of() STL Algorithm, which is introduced in c++11.

Need of std::all_of()

This STL algorithm is useful when you have a range/array of element and you want to check if all elements in given range satisfies a given condition. It’s just opposite to std::any_of().

std::all_of() Introduction

all_of() accepts a range of elements and a Unary Predicate (i.e a call back) as an argument.

bool all_of (<Start of Range>, <End of Range>, UnaryPredicate pred);

all_of() iterates over all the elements in given range and for each elements calls the given callback i.e. Unary Predicate.
If for any element then given predicate returns false then it stops the further iteration and return false, else it returns true.

std::all_of() usage details

How to use std::all_of() with two different type of callbacks i.e Lambda Function and Function pointer.

Using std::all_of() with Lambda Function

Suppose We have a vector of strings i.e.

// A vector of strings
std::vector<std::string> wordList = { "Hi", "Hello", "Test", "First", "Second", "Third", "Fourth" };

Now we want to check if all strings in vector are of same size i.e. 4 using std::all_of(),
/*
 Check if all strings in vector are of same size i.e. 4.
 */
bool result = std::all_of(wordList.begin(), wordList.end(), [](const std::string & str) {
																return str.size() == 4;
																});

std::all_of() will iterate through all the strings in vector and for each string in vector it calls the passed lambda function, that checks if size of string is 4. If for any string the lambda function returns false, std::all_of() will stop the further iteration and returns false else it returns true.

Now Check if all strings in vector starts with a Upper Case Letter using all_of(),

/*
 Check if all strings in vector starts with a Upper Case Letter
 */
result = std::all_of(wordList.begin(), wordList.end(), [](const std::string & str) {
												return str.size() > 0 && ::isupper(str[0]);
												});

Using std::all_of() with a Function Pointer

Suppose we have a string i.e.

std::string str = "testString";

Now let’s Check if given string contains all lower case letters i.e. not a single upper case char
/*
 Check if given string contains all lower case letters i.e. not a single upper case char
 */
result = std::all_of(str.begin(), str.end(), ::islower);

Using std::all_of() with Arrays

Suppose we have an array i.e.

int arr[] = {3,9,75,15,12};

Now we will check if all numbers in array are multiple of 3 using all_of() i.e.
// Check if all numbers in array are multiple of 3 i.e.
result = std::all_of(arr, arr + sizeof(arr)/ sizeof(int), [](const int & i){ return i%3 == 0; });

So, basically std::all_of() is to make our life easy.

Complete example is as follows,

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int main()
{
	// A vector of strings
	std::vector<std::string> wordList =
	{ "Hi", "Hello", "Test", "First", "Second", "Third", "Fourth" };

	/*
	 Check if all strings in vector are of same size i.e. 4.
	 */
	bool result = std::all_of(wordList.begin(), wordList.end(), [](const std::string & str) {
																	return str.size() == 4;
																	});

	std::cout << " Is Vector contains all string with size 4 ? | Result = " << result << std::endl;

	/*
	 Check if all strings in vector starts with a Upper Case Letter
	 */
	result = std::all_of(wordList.begin(), wordList.end(), [](const std::string & str) {
													return str.size() > 0 && ::isupper(str[0]);
													});

	std::cout<< "Are all strings in vector starts with a Upper Case Letter ? | Result = " << result << std::endl;

	std::string str = "testString";
	/*
	 Check if given string contains all lower case letters i.e. not a single upper case char
	 */
	result = std::all_of(str.begin(), str.end(), ::islower);

	std::cout << "str  = " << str << std::endl;
	std::cout << "Does given string contains all lower case letters ? | Result = " << result << std::endl;

	// Using all_of() with array

	int arr[] = {3,9,75,15,12};

	// Check if all numbers in array are multiple of 3 i.e.
	result = std::all_of(arr, arr + sizeof(arr)/ sizeof(int), [](const int & i){ return i%3 == 0; });

	std::cout << "Does all numbers in array are multiple of 3 | Result = " << result << std::endl;
	return 0;
}

Output:
 Is Vector contains all string with size 4 ? | Result = 0
Are all strings in vector starts with a Upper Case Letter ? | Result = 1
str  = testString
Does given string contains all lower case letters ? | Result = 0
Does all numbers in array are multiple of 3 | Result = 1

 

Join a list of 2000+ Programmers for latest Tips & Tutorials