C++11 – std::all_of () – Algorithm tutorial and example

In the previous article, we have discussed about C++: How to extract file extension from a path string using Boost & C++17 FileSystem Library. Let us learn how to find Algorithm tutorial in C++ Program.

std::all_of() – Algorithm tutorial & example

We are going to see how we can use the std::all_of( ) algorithm from C++11.

Need of std::all_of( ) :

This function is used when there is an array or a range of elements and we want to check if all the elements agree to a particular condition.

std::all_of() Introduction :

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

The all_of( ) takes three arguments, the start and end of the range and a unnarypredicate which is a callback element. The function runs over all the elements in the range and calls the Unary predicate. In case any of the elements doesn’t meet the conditions, the further iterations are stopped and the function returns false, else true.

std::all_of() usage details :

Using std::all_of() with Lambda Function :

We will test an array of strings and check whether all of them are of same size.

Below is the code implementation.

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

using namespace std;

int main(int argc, char const *argv[])
{
    vector<string> words = {"Hi", "Cry", "Bye", "Whole", "Ship"};

    bool res = std::all_of(words.begin(), words.end(), [](const std::string &str)
                           { return str.size() == 3; });
    cout << "Result = " << (res ? "true" : "false");
    return 0;
}

Output :
Result = false

The all_of( ) function checks individual elements passing them into the lambda function and here, as all the words were not of the same size(3), it returns false. We can also try to check for if all strings start with an uppercase letter.

Below is the code implementation.

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

using namespace std;

int main(int argc, char const *argv[])
{
    vector<string> words = {"Hi", "Cry", "Bye", "Whole", "Ship"};

    bool res = std::all_of(words.begin(), words.end(), [](const std::string &str)
                           { str.size() > 0 && ::isupper(str[0]); });
    cout << "Result = " << (res ? "true" : "false");
    return 0;
}
Output :
Result = true

Using std::all_of() with a Function Pointer :

Below is the code implementation.

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

using namespace std;

int main(int argc, char const *argv[])
{
    string str = "randomstring";

    bool res = std::all_of(str.begin(), str.end(), ::islower);
    cout << "Result = " << (res ? "true" : "false");
    return 0;
}

Output :
Result = true

Using std::all_of() with Arrays :

Below is the code implementation.

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

using namespace std;

int main(int argc, char const *argv[])
{
    int arr = [ 10, 20, 30, 40 ];

    bool res = std::all_of(arr, arr + sizeof(arr) / sizeof(int), [](const int &i)
                           { return i % 3 == 0; });
    cout << "Result = " << (res ? "true" : "false");
    return 0;
}
Output :
Result = false

The above function checks whether all the values are divisible by 3 or not.