forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_examples.cpp
More file actions
58 lines (46 loc) · 1.34 KB
/
filter_examples.cpp
File metadata and controls
58 lines (46 loc) · 1.34 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
#include <filter.hpp>
#include <vector>
#include <iostream>
bool greater_than_four(int i) {
return i > 4;
}
class LessThanValue {
private:
int compare_val;
public:
LessThanValue() = delete;
LessThanValue(int v) : compare_val(v) { }
bool operator() (int i) {
return i < this->compare_val;
}
};
int main() {
std::vector<int> ns{1, 5, 6, 0, 7, 2, 3, 8, 3, 0, 2, 1};
std::cout << "ns = { ";
for (auto&& i : ns) {
std::cout << i << ' ';
}
std::cout << "}\n";
std::cout << "Greater than 4 (function pointer)\n";
for (auto&& i : iter::filter(greater_than_four, ns)) {
std::cout << i << '\n';
}
std::cout << "Less than 4 (lambda)\n";
for (auto&& i : iter::filter([] (const int i) { return i < 4; }, ns)) {
std::cout << i << '\n';
}
LessThanValue lv(4);
std::cout << "Less than 4 (callable object)\n";
for (auto&& i : iter::filter(lv, ns)) {
std::cout << i << '\n';
}
// filter(seq) with no predicate uses the truthiness of the values
std::cout << "Nonzero ints filter(ns)\n";
for (auto&& i : iter::filter(ns)) {
std::cout << i << '\n';
}
std::cout << "odd numbers\n";
for (auto&& i : iter::filter([] (const int i) {return i % 2;}, ns)) {
std::cout << i << '\n';
}
}