diff --git a/filter_iterator.htm b/filter_iterator.htm new file mode 100644 index 0000000..e662116 --- /dev/null +++ b/filter_iterator.htm @@ -0,0 +1,104 @@ + + + + + + +Filter Iterator Adaptor Documentation + + + + +c++boost.gif (8819 bytes) + +

Filter Iterator Adaptor

+ +Defined in header +boost/iterator_adaptors.hpp + + +

+The filter iterator adaptor ... + + + +

Example

+ +The following example uses filter iterator to print out all the +positive integers in an array. + +
+#include <boost/config.hpp>
+#include <algorithm>
+#include <iostream>
+#include <boost/iterator_adaptors.hpp>
+
+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
+
+ + +The filter iterator adaptors +models ForwardIterator + + + +

The Filter Iterator Type Generator

+ +
+template <class Predicate, 
+          class Iterator,
+          class Value = value default,
+          class Pointer = pointer default,
+          class Reference = reference default,
+          class Category = category default,
+          class Distance = difference type default>
+class filter_iterator_generator
+{
+  typedef ... type;
+  typedef ... policies_type;
+}
+
+ +

+show another example and explain defaults... + + +

The Make Filter Iterator Helper Function

+ +
+template <class Predicate, class Iterator>
+inline typename detail::filter_generator<Predicate, Iterator>::type
+make_filter_iterator(Iterator first, Iterator last, const Predicate& p = Predicate())
+
+ + +
+

Revised 10 Feb 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.

+ + + + diff --git a/filter_iterator_example.cpp b/filter_iterator_example.cpp new file mode 100644 index 0000000..e4bd755 --- /dev/null +++ b/filter_iterator_example.cpp @@ -0,0 +1,30 @@ +// Example of using the filter iterator adaptor from +// boost/iterator_adaptors.hpp. + +// (C) Copyright Jeremy Siek 1999. Permission to copy, use, modify, +// sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. + + +#include +#include +#include +#include + +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(numbers, numbers + N), + boost::make_filter_iterator(numbers + N, numbers + N), + std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + return 0; +}