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)
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``.
15 struct is_positive_number {
16 bool operator()(int x) { return 0 < x; }
21 int numbers_[] = { 0, -1, 4, -3, 5, 8, -2 };
22 const int N = sizeof(numbers_)/sizeof(int);
24 typedef int* base_iterator;
25 base_iterator numbers(numbers_);
27 // Example using filter_iterator
28 typedef boost::filter_iterator<is_positive_number, base_iterator>
31 is_positive_number predicate;
32 FilterIter filter_iter_first(predicate, numbers, numbers + N);
33 FilterIter filter_iter_last(predicate, numbers + N, numbers + N);
35 std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator<int>(std::cout, " "));
36 std::cout << std::endl;
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;
44 // Another example using make_filter_iterator()
46 boost::make_filter_iterator(
47 std::bind2nd(std::greater<int>(), -2)
48 , numbers, numbers + N)
50 , boost::make_filter_iterator(
51 std::bind2nd(std::greater<int>(), -2)
52 , numbers + N, numbers + N)
54 , std::ostream_iterator<int>(std::cout, " ")
57 std::cout << std::endl;
59 return boost::exit_success;
70 The source code for this example can be found `here`__.
72 __ ../example/filter_iterator_example.cpp