]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | .. Copyright David Abrahams 2006. Distributed under the Boost |
2 | .. Software License, Version 1.0. (See accompanying | |
3 | .. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
4 | ||
5 | Example | |
6 | ....... | |
7 | ||
8 | This example uses ``filter_iterator`` and then | |
9 | ``make_filter_iterator`` to output only the positive integers from an | |
10 | array of integers. Then ``make_filter_iterator`` is is used to output | |
11 | the integers greater than ``-2``. | |
12 | ||
13 | :: | |
14 | ||
15 | struct is_positive_number { | |
16 | bool operator()(int x) { return 0 < x; } | |
17 | }; | |
18 | ||
19 | int main() | |
20 | { | |
21 | int numbers_[] = { 0, -1, 4, -3, 5, 8, -2 }; | |
22 | const int N = sizeof(numbers_)/sizeof(int); | |
23 | ||
24 | typedef int* base_iterator; | |
25 | base_iterator numbers(numbers_); | |
26 | ||
27 | // Example using filter_iterator | |
28 | typedef boost::filter_iterator<is_positive_number, base_iterator> | |
29 | FilterIter; | |
30 | ||
31 | is_positive_number predicate; | |
32 | FilterIter filter_iter_first(predicate, numbers, numbers + N); | |
33 | FilterIter filter_iter_last(predicate, numbers + N, numbers + N); | |
34 | ||
35 | std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator<int>(std::cout, " ")); | |
36 | std::cout << std::endl; | |
37 | ||
38 | // Example using make_filter_iterator() | |
39 | std::copy(boost::make_filter_iterator<is_positive_number>(numbers, numbers + N), | |
40 | boost::make_filter_iterator<is_positive_number>(numbers + N, numbers + N), | |
41 | std::ostream_iterator<int>(std::cout, " ")); | |
42 | std::cout << std::endl; | |
43 | ||
44 | // Another example using make_filter_iterator() | |
45 | std::copy( | |
46 | boost::make_filter_iterator( | |
47 | std::bind2nd(std::greater<int>(), -2) | |
48 | , numbers, numbers + N) | |
49 | ||
50 | , boost::make_filter_iterator( | |
51 | std::bind2nd(std::greater<int>(), -2) | |
52 | , numbers + N, numbers + N) | |
53 | ||
54 | , std::ostream_iterator<int>(std::cout, " ") | |
55 | ); | |
56 | ||
57 | std::cout << std::endl; | |
58 | ||
59 | return boost::exit_success; | |
60 | } | |
61 | ||
62 | ||
63 | The output is:: | |
64 | ||
65 | 4 5 8 | |
66 | 4 5 8 | |
67 | 0 -1 4 5 8 | |
68 | ||
69 | ||
70 | The source code for this example can be found `here`__. | |
71 | ||
72 | __ ../example/filter_iterator_example.cpp |