-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_of.cpp
More file actions
60 lines (49 loc) · 1.89 KB
/
all_of.cpp
File metadata and controls
60 lines (49 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// http://www.cplusplus.com/reference/algorithm/all_of/
// GB modified to illustrate iterators and bind
// all_of example
#include <iostream> // std::cout
#include <algorithm> // std::all_of
#include <array> // std::array
#include <functional> // std::bind
#include <typeinfo> // typeid
#define TYPEID(X) std::cout << "typeid(" #X ").name()=" << std::typeid(X).name() << "\n";
int main () {
std::array<int,8> foo = {3,5,7,11,13,17,19,23};
std::cout << "Do it with std::all_of (a loop)\n";
if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
std::cout << "All the elements are odd numbers.\n";
std::cout << "Do it with std::all_of (a loop), printing each value(x) passed to f\n";
auto f0 = [](int x) -> bool {return x%2;};
auto f = [](int x) -> bool {bool r = x%2; std::cout <<"x= " << x << ",r=" << r << " "; return r;};
if ( std::all_of(foo.begin(), foo.end(), f) )
std::cout << "All the elements are odd numbers.\n";
std::cout << "Now do it with a for loop\n";
bool all_odd = true;
for(auto it = foo.begin(); it != foo.end(); it++) {
if(f(*it) == 0) {
all_odd = false;
break;
}
}
if(all_odd)
std::cout << "All the elements are odd numbers.\n";
std::cout << "Now do it with a for loop using bind\n";
all_odd = true;
for(auto it = foo.begin(); it != foo.end(); it++) {
auto b = std::bind(f,*it);
if(b() == 0) {
all_odd = false;
break;
}
}
if(all_odd)
std::cout << "All the elements are odd numbers.\n";
// TYPEID(std::all_of, foo.begin(), foo.end(), f);
// TYPEID(std::bind(std::all_of, foo.begin(), foo.end(), f)); // syntax error
std::cout << "bind... = "
<< "std::bind(std::all_of, foo.begin(), foo.end(), f)"
// << std::bind(std::all_of, foo.begin(), foo.end(), f) // syntax error
<< " does not compile"
<< "\n";
return 0;
}