We will learn about different ways to check if all elements in a vector are true or not in C++.

Check if all elements in vector are true using STL’s all_of()

The Standard Template Library in C++ provides a function all_of(), and it checks if a given predicate/callback function returns true for all the elements of a sequence or not. To check if all the elements in a vector are true, we will pass the following arguments to the all_of() function,

  • Iterator pointing to the beginning of vector.
  • Iterator pointing to the end of vector.
  • A Lambda function, which accepts a bool value as an argument and returns true if the given value is true.

The all_of() function will call the given lambda function for each element of the vector. It will return true if the lambda function returns true for all the vector elements.

Let’s see the complete example,

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

int main()
{
    // Create vector boolean values
    std::vector<bool> vecObj = {true, true, true, true, true};

    // Check if all elements of a vector are true
    if (std::all_of(vecObj.begin(),
                    vecObj.end(),
                    [] (const bool& elem) {
                        return elem;
                    }))
    {
        std::cout<<"All elements of a vector are true" <<std::endl;
    }
    else
    {
        std::cout<<"All elements of a vector are not true" <<std::endl;
    }

    return 0;
}

Output:

All elements of a vector are true

The lambda function returned true for all the vector elements, therefore all_of() returned true. It means all elements in the vector are true.

Check if all elements in vector are true using for loop

Iterate over all elements of vector using a for loop, and for each element check if it is true or not. As soon as you encounter a value that is false, set the result flag to false and break the loop, because it means all the values in vector are not true.

Let’s see the complete example,

#include <iostream>
#include <vector>

int main()
{
    // Create vector boolean values
    std::vector<bool> vecObj = {true, true, true, true, true};

    bool result = true;

    // Check if all elements of a vector are true
    for (const bool& elem: vecObj)
    {
        if (!elem)
        {
            result = false;
            break; 
        }
    }

    if (result)
    {
        std::cout<<"All elements of a vector are true" <<std::endl;
    }
    else
    {
        std::cout<<"All elements of a vector are not true" <<std::endl;
    }

    return 0;
}

Output:

All elements of a vector are true

It confirmed that the vector has no false value.

Check if all elements in vector are true using STL’s find_if()

We can also use the find_if() function of STL, to check if there is any false value in the vector or not. For that we need to pass the following arguments to the find_if() function,

  • Iterator pointing to the beginning of vector.
  • Iterator pointing to the end of vector.
  • A Lambda function, which accepts a bool value as an argument and returns true if the given value is false.

It will return an iterator pointing to the first false value in vector. If there is no false value in the vector then it will return an iterator pointing to the end of vector.

Let’s see the complete example,

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

int main()
{
    // Create vector boolean values
    std::vector<bool> vecObj = {true, true, true, true, true};

    // Search of a false value in the vector
    auto it = std::find_if(
                    vecObj.begin(),
                    vecObj.end(),
                    [&](const bool& elem){
                        return !elem;
                    }) ;

    // Check if all values in vector are true
    if (it == vecObj.end())
    {
        std::cout<<"All elements of a vector are true" <<std::endl;
    }
    else
    {
        std::cout<<"All elements of a vector are not true" <<std::endl;
    }

    return 0;
}

Output:

All elements of a vector are true

It confirmed that the vector has no false value.

Summary

We learned how to check if all the values in a vector are true.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

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