#include <algorithm>
#include <iostream>
int main()
{// 0 1 2 3 4
int allOnes[] = {1, 1, 1, 1, 1};
int someOnes[] = {1, 1, 1, 1, 0};
int * beginIter = &allOnes[0];
int * endIter = &allOnes[5];
bool allAreOnes = std::all_of (beginIter, endIter,
[](int element) {return element == 1;});
std::cout << allAreOnes << " ";
bool someAreOnes = std::any_of (beginIter, endIter,
[](int element) {return element == 1;});
std::cout << someAreOnes << " ";
bool noneAreOnes = std::none_of(beginIter, endIter,
[](int element) {return element == 1;});
std::cout << noneAreOnes << " : ";
beginIter = &someOnes[0];
endIter = &someOnes[5];
allAreOnes = std::all_of (beginIter, endIter,
[](int element) {return element == 1;});
std::cout << allAreOnes << " ";
someAreOnes = std::any_of (beginIter, endIter,
[](int element) {return element == 1;});
std::cout << someAreOnes << " ";
noneAreOnes = std::none_of(beginIter, endIter,
[](int element) {return element == 1;});
std::cout << noneAreOnes << " ";
return 0;
}
// Output: 1 1 0 : 0 1 0
Reference: https://isocpp.org/wiki/faq/cpp11-library-stl#cpp11-algorithms
Friday, September 1, 2017
C++11: Algorithms: all_of, any_of, none_of
Given first and last iterators and a predicate, you can test to see if all, any or none of the elements in a container satisfies the predicates. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment