Overview
This article shows the simple example ofall_of
from algorithm
library on C++.Example
The following code is one of the example of usingall_of
.#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool less_than_two(int i){
return i < 2;
}
bool less_than_four(int i){
return i < 4;
}
int main(){
vector<int> a{1,2,3};
if (all_of(a.begin(), a.end(), less_than_two)){
cout << "All the values are less than two." << endl;
} else {
cout << "Not all the values are less than two." << endl;
}
if (all_of(a.begin(), a.end(), less_than_four)){
cout << "All the values are less than four." << endl;
} else {
cout << "Not all the values are less than four." << endl;
}
return 0;
}
On this code, it is checking if all the value of variable,
a
, are less than two and are less than four. The variable a
contains 1
, 2
and 3
. So, not everything are less than 2 but everything are less than four.Syntax
all_of
takes three arguments, target range’s initial position iterator, final position iterator and unary function. This unary function takes one argument and return bool or something which can be converted to bool.all_of(input_initial_iterator, input_final_iterator, unary_fnction)