# Filter Iterator Adaptor

Defined in header boost/iterator_adaptors.hpp

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.

## Synopsis

    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());
    }

## The Filter Iterator Type Generator

The class `filter_iterator_generator` is a helper class whose purpose is to construct a filter iterator type. The template parameters for this class are the `Predicate` function object type and the `BaseIterator` type that is being wrapped. In most cases the associated types for the wrapped iterator can be deduced from `std::iterator_traits`, but in some situations the user may want to override these types, so there are also template parameters for each of the iterator's associated types.

    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
    }

### Example

The following example uses filter iterator to print out all the positive integers in an array.

    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

### Template Parameters

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`

### Model of

The filter iterator adaptor (the type `filter_iterator_generator<...>::type`) may be a model of InputIterator or ForwardIterator depending on the adapted iterator type.

### Members

The filter iterator type implements all of the member functions and operators required of the ForwardIterator concept. In addition it has the following constructor:

    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)

## The Make Filter Iterator Function

    template <class Predicate, class BaseIterator>
    typename 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.

### Example

In this example we print out all numbers in the array that are greater than negative two.

    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::greater(), -2)),
    	    boost::make_filter_iterator(numbers + N, numbers + N,
    					std::bind2nd(std::greater(), -2)),
    	    std::ostream_iterator(std::cout, " "));
      std::cout << std::endl;

    }

The output is:

    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

### Notes

[1] If the compiler does not support partial specialization and the wrapped iterator type is a builtin pointer then the `Value` type must be explicitly specified (don't use the default).

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.