Skip to content

Commit 4025529

Browse files
committed
replaces testfilter with filter_examples
1 parent 535c0a6 commit 4025529

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

examples/filter_examples.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <filter.hpp>
2+
3+
#include <vector>
4+
#include <iostream>
5+
6+
bool greater_than_four(int i) {
7+
return i > 4;
8+
}
9+
10+
class LessThanValue {
11+
private:
12+
int compare_val;
13+
14+
public:
15+
LessThanValue() = delete;
16+
LessThanValue(int v) : compare_val(v) { }
17+
18+
bool operator() (int i) {
19+
return i < this->compare_val;
20+
}
21+
};
22+
23+
24+
int main() {
25+
std::vector<int> ns{1, 5, 6, 0, 7, 2, 3, 8, 3, 0, 2, 1};
26+
std::cout << "ns = { ";
27+
for (auto&& i : ns) {
28+
std::cout << i << ' ';
29+
}
30+
std::cout << "}\n";
31+
32+
std::cout << "Greater than 4 (function pointer)\n";
33+
for (auto&& i : iter::filter(greater_than_four, ns)) {
34+
std::cout << i << '\n';
35+
}
36+
37+
std::cout << "Less than 4 (lambda)\n";
38+
for (auto&& i : iter::filter([] (const int i) { return i < 4; }, ns)) {
39+
std::cout << i << '\n';
40+
}
41+
42+
LessThanValue lv(4);
43+
std::cout << "Less than 4 (callable object)\n";
44+
for (auto&& i : iter::filter(lv, ns)) {
45+
std::cout << i << '\n';
46+
}
47+
48+
// filter(seq) with no predicate uses the truthiness of the values
49+
std::cout << "Nonzero ints filter(ns)\n";
50+
for (auto&& i : iter::filter(ns)) {
51+
std::cout << i << '\n';
52+
}
53+
54+
std::cout << "odd numbers\n";
55+
for (auto&& i : iter::filter([] (const int i) {return i % 2;}, ns)) {
56+
std::cout << i << '\n';
57+
}
58+
}

0 commit comments

Comments
 (0)