The filter iterator adaptor creates a view of an iterator range in which some elements of the range are skipped over. A Predicate function object controls which elements are skipped. When the predicate is applied to an element, if it returns true then the element is retained and if it returns false then the element is skipped over.
namespace boost { template <class Predicate, class BaseIterator, ...> class filter_iterator_generator; template <class Predicate, class BaseIterator> typename filter_iterator_generator<Predicate, BaseIterator>::type make_filter_iterator(BaseIterator first, BaseIterator last, const Predicate& p = Predicate()); }
template <class Predicate, class BaseIterator, class Value, class Reference, class Pointer, class Category, class Distance> class filter_iterator_generator { public: typedef iterator_adaptor<...> type; // the resulting filter iterator type }
struct is_positive_number { bool operator()(int x) { return 0 < x; } }; int main() { int numbers[] = { 0, -1, 4, -3, 5, 8, -2 }; const int N = sizeof(numbers)/sizeof(int); typedef boost::filter_iterator_generator<is_positive_number, int*, int>::type FilterIter; is_positive_number predicate; FilterIter::policies_type policies(predicate, numbers + N); FilterIter filter_iter_first(numbers, policies); FilterIter filter_iter_last(numbers + N, policies); std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; return 0; }The output is:
4 5 8
Parameter | Description |
---|---|
Predicate | The function object that determines which elements are retained and which elements are skipped. |
BaseIterator | The iterator type being wrapped. This type must at least be a model of the InputIterator concept. |
Value | The value_type of the resulting iterator,
unless const. If const, a conforming compiler strips constness for the
value_type. Typically the default for this parameter is the
appropriate type[1]. Default: std::iterator_traits<BaseIterator>::value_type |
Reference | The reference type of the resulting iterator, and in
particular, the result type of operator*(). Typically the default for
this parameter is the appropriate type. Default: If Value is supplied, Value& is used. Otherwise std::iterator_traits<BaseIterator>::reference is used. |
Pointer | The pointer type of the resulting iterator, and in
particular, the result type of operator->().
Typically the default for
this parameter is the appropriate type. Default: If Value was supplied, then Value*, otherwise std::iterator_traits<BaseIterator>::pointer. |
Category | The iterator_category type for the resulting iterator.
Typically the
default for this parameter is the appropriate type. If you override
this parameter, do not use bidirectional_iterator_tag
because filter iterators can not go in reverse. Default: std::iterator_traits<BaseIterator>::iterator_category |
Distance | The difference_type for the resulting iterator. Typically the default for
this parameter is the appropriate type. Default: std::iterator_traits<BaseIterator>::difference_type |
filter_iterator_generator::type(const BaseIterator& it, const Policies& p = Policies())
The policies type has only one public function, which is its constructor:
filter_iterator_generator::policies_type(const Predicate& p, const BaseIterator& end)
template <class Predicate, class BaseIterator> typename detail::filter_generator<Predicate, BaseIterator>::type make_filter_iterator(BaseIterator first, BaseIterator last, const Predicate& p = Predicate())This function provides a convenient way to create filter iterators.
int main() { int numbers[] = { 0, -1, 4, -3, 5, 8, -2 }; const int N = sizeof(numbers)/sizeof(int); std::copy(boost::make_filter_iterator(numbers, numbers + N, std::bind2nd(std::greaterThe output is:(), -2)), boost::make_filter_iterator(numbers + N, numbers + N, std::bind2nd(std::greater (), -2)), std::ostream_iterator (std::cout, " ")); std::cout << std::endl; }
0 -1 4 5 8
In the next example we print the positive numbers using the make_filter_iterator() function.
struct is_positive_number { bool operator()(int x) { return 0 < x; } }; int main() { int numbers[] = { 0, -1, 4, -3, 5, 8, -2 }; const int N = sizeof(numbers)/sizeof(int); std::copy(boost::make_filter_iterator<is_positive_number>(numbers, numbers + N), boost::make_filter_iterator<is_positive_number>(numbers + N, numbers + N), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; return 0; }The output is:
4 5 8
Revised 09 Mar 2001
© Copyright Jeremy Siek 2000. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.