std::all_of() in C++

The std::all_of() function is a STL Algorithm in C++. It can be used to check if all the elements of a sequence satisfies a condition or not. The sequence can be a vector, array, list or any other sequential container.

We need to include the <algorithm> header file to use the std::all_of() function.

Syntax of std::all_of()

template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);

Parameters of std::all_of()

It accepts three arguments,

  • first: An Iterator pointing to the start of sequence.
  • last: An Iterator pointing to the end of sequence.
  • pred: A callback function.
    • It will be a unary function, which accepts an element from range as argument, and returns a bool value.
    • It basically checks if the given element satisfies the given condition or not.
    • It can be a Lambda function or a function pointer or a function object.

The sequence can be a std::vector, array, std::list, std::array or any other sequential container.

Return value of std::all_of()

  • It returns a bool value.
    • It applies all the given callback function (Unary Predicate) on all the elements of sequence. If this callback function returns true for all the elements of sequence, then the std::all_of() also returns true, otherwise it returns false.

Examples of std::all_of() in C++

Using std::all_of() with vector & Lambda function

Suppose we have a vector of integers, and we want to check if all the numbers in vector are even numbers. For this we can use the std::all_of() function. We can pass three arguments in the std::all_of() i.e.

  • Iterator pointing to the start of vector.
  • Iterator pointing to the end of vector.
  • A lambda function which accepts an integer, and returns true if the given integer is even number.

The std::all_of() applied the givend lambda function on all the elements of vector. If this lambda function returns true for all the elements of vector, then it means all elements of vector satisfies the condition i.e. all elements are even in vector. In that case std::all_of() will return true.

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

int main ()
{
    std::vector<int> numbers = {22, 44, 46, 68, 90, 88, 36};

    // check if all elements of vector are even numbers
    bool result = std::all_of(
                        numbers.begin(),
                        numbers.end(),
                        [](const int& num){
                            // Check if given number is even
                            return num % 2 == 0;
                        });

    if ( result )
    {
        std::cout<<"All integers in Vector are even numbers. \n";
    }
    else
    {
        std::cout<<"All integers in Vector are not even numbers. \n";
    }

  return 0;
}

Output:

All integers in Vector are even numbers. 

Here, we used the std::all_of() to check if all elements of vector are even numbers.

Using std::all_of() with array & function pointer

Suppose we have an array of integers, and we want to check if all the numbers in array are even numbers. For this we can use the std::all_of() function just like the previous solution. But we will use a function pointer instead of lambda function.

#include <iostream>
#include <algorithm>

// A Unary Function
bool condition(const int& num)
{
    // Check if given number is even
    return num % 2 == 0;
}

int main ()
{
    int arr[] = {22, 44, 46, 68, 90, 88, 36};

    // check if all elements of array are even numbers
    bool result = std::all_of(
                        std::begin(arr),
                        std::end(arr),
                        &condition);

    if ( result )
    {
        std::cout<<"All integers in array are even numbers. \n";
    }
    else
    {
        std::cout<<"All integers in array are not even numbers. \n";
    }

  return 0;
}

Output:

All integers in array are even numbers. 

Here, we used the std::all_of() to check if all elements of an array are even numbers.

Summary

We learned about the usage details of std::all_of() function in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top