diff --git a/counting_iterator.htm b/counting_iterator.htm deleted file mode 100644 index 0f6cb64..0000000 --- a/counting_iterator.htm +++ /dev/null @@ -1,325 +0,0 @@ - - - - - - -Counting Iterator Adaptor Documentation - - - - -c++boost.gif (8819 bytes) - -

Counting Iterator Adaptor

- -Defined in header -boost/counting_iterator.hpp - -

-How would you fill up a vector with the numbers zero -through one hundred using std::copy()? The -only iterator operation missing from builtin integer types is an -operator*() that returns the current -value of the integer. The counting iterator adaptor adds this crucial piece of -functionality to whatever type it wraps. One can use the -counting iterator adaptor not only with integer types, but with any -type that is Incrementable (see type requirements below). The -following pseudo-code shows the general idea of how the -counting iterator is implemented. -

- -
-  // inside a hypothetical counting_iterator class...
-  typedef Incrementable value_type;
-  value_type counting_iterator::operator*() const {
-    return this->base; // no dereference!
-  }
-
- -All of the other operators of the counting iterator behave in the same -fashion as the Incrementable base type. - -

Synopsis

- -
-namespace boost {
-  template <class Incrementable>
-  struct counting_iterator_traits;
-
-  template <class Incrementable>
-  struct counting_iterator_generator;
-
-  template <class Incrementable>
-  typename counting_iterator_generator<Incrementable>::type
-  make_counting_iterator(Incrementable x);
-}
-
- -
- -

The Counting Iterator Type -Generator

- -The class template counting_iterator_generator<Incrementable> is a type generator for counting iterators. - -
-template <class Incrementable>
-class counting_iterator_generator
-{
-public:
-    typedef iterator_adaptor<...> type;
-};
-
- -

Example

- -In this example we use the counting iterator generator to create a -counting iterator, and count from zero to four. - -
-#include <boost/config.hpp>
-#include <iostream>
-#include <boost/counting_iterator.hpp>
-
-int main(int, char*[])
-{
-  // Example of using counting_iterator_generator
-  std::cout << "counting from 0 to 4:" << std::endl;
-  boost::counting_iterator_generator<int>::type first(0), last(4);
-  std::copy(first, last, std::ostream_iterator<int>(std::cout, " "));
-  std::cout << std::endl;
-
-  // to be continued...
-
-The output from this part is: -
-counting from 0 to 4:
-0 1 2 3 
-
- -

Template Parameters

- - - - - - - - - - - -
ParameterDescription
IncrementableThe type being wrapped by the adaptor.
- -

Model of

- -If the Incrementable type has all of the functionality of a -Random -Access Iterator except the operator*(), then the counting -iterator will be a model of Random -Access Iterator. If the Incrementable type has less -functionality, then the counting iterator will have correspondingly -less functionality. - -

Type Requirements

- -The Incrementable type must be Default -Constructible, Copy -Constructible, and Assignable. -Also, the Incrementable type must provide access to an -associated difference_type and iterator_category -through the counting_iterator_traits -class. - -

-Furthermore, if you wish to create a counting iterator that is a Forward -Iterator, then the following expressions must be valid: -

-Incrementable i, j;
-++i         // pre-increment
-i == j      // operator equal
-
-If you wish to create a counting iterator that is a -Bidirectional Iterator, then pre-decrement is also required: -
---i
-
-If you wish to create a counting iterator that is a Random -Access Iterator, then these additional expressions are also required: -
-counting_iterator_traits<Incrementable>::difference_type n;
-i += n
-n = i - j
-i < j
-
- - - -

Members

- -The counting iterator type implements the member functions and -operators required of the Random -Access Iterator concept. In addition it has the following -constructor: - -
-counting_iterator_generator::type(const Incrementable& i)
-
- -

-


-

- - -

The Counting Iterator Object Generator

- -
-template <class Incrementable>
-typename counting_iterator_generator<Incrementable>::type
-make_counting_iterator(Incrementable base);
-
- -An object -generator function that provides a convenient way to create counting -iterators.

- - - -

Example

- -In this example we count from negative five to positive five, this -time using the make_counting_iterator() function to save some -typing. - -
-  // continuing from previous example...
-
-  std::cout << "counting from -5 to 4:" << std::endl;
-  std::copy(boost::make_counting_iterator(-5),
-	    boost::make_counting_iterator(5),
-	    std::ostream_iterator<int>(std::cout, " "));
-  std::cout << std::endl;
-
-  // to be continued...
-
-The output from this part is: -
-counting from -5 to 4:
--5 -4 -3 -2 -1 0 1 2 3 4 
-
- -In the next example we create an array of numbers, and then create a -second array of pointers, where each pointer is the address of a -number in the first array. The counting iterator makes it easy to do -this since dereferencing a counting iterator that is wrapping an -iterator over the array of numbers just returns a pointer to the -current location in the array. We then use the indirect iterator adaptor to print -out the number in the array by accessing the numbers through the array -of pointers. - -
-  // continuing from previous example...
-
-  const int N = 7;
-  std::vector<int> numbers;
-  // Fill "numbers" array with [0,N)
-  std::copy(boost::make_counting_iterator(0), boost::make_counting_iterator(N),
-	    std::back_inserter(numbers));
-
-  std::vector<std::vector<int>::iterator> pointers;
-
-  // Use counting iterator to fill in the array of pointers.
-  std::copy(boost::make_counting_iterator(numbers.begin()),
-	    boost::make_counting_iterator(numbers.end()),
-	    std::back_inserter(pointers));
-
-  // Use indirect iterator to print out numbers by accessing
-  // them through the array of pointers.
-  std::cout << "indirectly printing out the numbers from 0 to " 
-	    << N << std::endl;
-  std::copy(boost::make_indirect_iterator(pointers.begin()),
-	    boost::make_indirect_iterator(pointers.end()),
-	    std::ostream_iterator<int>(std::cout, " "));
-  std::cout << std::endl;
-
-The output is: -
-indirectly printing out the numbers from 0 to 7
-0 1 2 3 4 5 6 
-
- -
- -

Counting Iterator Traits

- -The counting iterator adaptor needs to determine the appropriate -difference_type and iterator_category to use based on the -Incrementable type supplied by the user. The -counting_iterator_traits class provides these types. If the -Incrementable type is an integral type or an iterator, these types -will be correctly deduced by the counting_iterator_traits provided by -the library. Otherwise, the user must specialize -counting_iterator_traits for her type or add nested typedefs to -her type to fulfill the needs of - -std::iterator_traits. - -

The following pseudocode describes how the counting_iterator_traits are determined: - -

-template <class Incrementable>
-struct counting_iterator_traits
-{
-  if (numeric_limits<Incrementable>::is_specialized) {
-    if (!numeric_limits<Incrementable>::is_integer)
-       COMPILE_TIME_ERROR;
-
-    if (!numeric_limits<Incrementable>::is_bounded
-        && numeric_limits<Incrementable>::is_signed) {
-        typedef Incrementable difference_type;
-    }
-    else if (numeric_limits<Incrementable>::is_integral) {
-        typedef next-larger-signed-type-or-intmax_t difference_type;
-    }
-    typedef std::random_access_iterator_tag iterator_category;   
-  } else {
-    typedef std::iterator_traits<Incrementable>::difference_type difference_type;
-    typedef std::iterator_traits<Incrementable>::iterator_category iterator_category;
-  }
-};
-
- -

The italicized sections above are implementation details, but it is important -to know that the difference_type for integral types is selected so that -it can always represent the difference between two values if such a built-in -integer exists. On platforms with a working std::numeric_limits -implementation, the difference_type for any variable-length signed -integer type T is T itself. - -


-

Revised 19 Aug 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.htm b/filter_iterator.htm deleted file mode 100644 index 6c0c973..0000000 --- a/filter_iterator.htm +++ /dev/null @@ -1,273 +0,0 @@ - - - - - - -Filter Iterator Adaptor Documentation - - - - -c++boost.gif (8819 bytes) - -

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ParameterDescription
PredicateThe function object that determines which elements are retained and which elements are skipped. -
BaseIteratorThe iterator type being wrapped. This type must at least be a model - of the InputIterator concept.
ValueThe 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
ReferenceThe 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.
PointerThe 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.
CategoryThe 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
DistanceThe 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.

- - - - diff --git a/function_output_iterator.htm b/function_output_iterator.htm deleted file mode 100644 index 6061a7b..0000000 --- a/function_output_iterator.htm +++ /dev/null @@ -1,169 +0,0 @@ - - - - - - - - - - Function Output Iterator Adaptor Documentation - - - - - c++boost.gif (8819 bytes) - -

Function Output Iterator Adaptor

- Defined in header boost/function_output_iterator.hpp - -

The function output iterator adaptor makes it easier to create - custom output iterators. The adaptor takes a Unary - Function and creates a model of Output - Iterator. Each item assigned to the output iterator is passed - as an argument to the unary function. The motivation for this - iterator is that creating a C++ Standard conforming output - iterator is non-trivial, particularly because the proper - implementation usually requires a proxy object. On the other hand, - creating a function (or function object) is much simpler. - -

Synopsis

- -
-
-namespace boost {
-  template <class UnaryFunction>
-  class function_output_iterator;
-
-  template <class UnaryFunction>
-  function_output_iterator<UnaryFunction>
-  make_function_output_iterator(const UnaryFunction& f = UnaryFunction())
-}
-
-
- -

Example

- - In this example we create an output iterator that appends - each item onto the end of a string, using the string_appender - function. - -
-
-#include <iostream>
-#include <string>
-#include <vector>
-
-#include <boost/function_output_iterator.hpp>
-
-struct string_appender {
-  string_appender(std::string& s) : m_str(s) { }
-  void operator()(const std::string& x) const {
-    m_str += x;
-  }
-  std::string& m_str;
-};
-
-int main(int, char*[])
-{
-  std::vector<std::string> x;
-  x.push_back("hello");
-  x.push_back(" ");
-  x.push_back("world");
-  x.push_back("!");
-
-  std::string s = "";
-  std::copy(x.begin(), x.end(), 
-            boost::make_function_output_iterator(string_appender(s)));
-  
-  std::cout << s << std::endl;
-
-  return 0;
-}
-
-
- -
- -

The Function Output Iterator Class

- -
-
-template <class UnaryFunction>
-class function_output_iterator;
-
-
- - The function_output_iterator class creates an Output - Iterator out of a - Unary - Function. Each item assigned to the output iterator is passed - as an argument to the unary function. - -

Template Parameters

- - - - - -
Parameter - - Description - -
UnaryFunction - - The function type being wrapped. The return type of the - function is not used, so it can be void. The - function must be a model of Unary - Function.
- -

Concept Model

- The function output iterator class is a model of Output - Iterator. - -

Members

- The function output iterator implements the member functions - and operators required of the Output - Iterator concept. In addition it has the following constructor: -
-explicit function_output_iterator(const UnaryFunction& f = UnaryFunction())
-
-
-
- -
-

The Function Output Iterator Object - Generator

- - The make_function_output_iterator() function provides a - more convenient way to create function output iterator objects. The - function saves the user the trouble of explicitly writing out the - iterator types. If the default argument is used, the function - type must be provided as an explicit template argument. - -
-
-template <class UnaryFunction>
-function_output_iterator<UnaryFunction>
-make_function_output_iterator(const UnaryFunction& f = UnaryFunction())
-
-
- -
- -

© Copyright Jeremy Siek 2001. 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/indirect_iterator.htm b/indirect_iterator.htm deleted file mode 100644 index a318424..0000000 --- a/indirect_iterator.htm +++ /dev/null @@ -1,444 +0,0 @@ - - - - - - - - - - Indirect Iterator Adaptor Documentation - - - - - c++boost.gif (8819 bytes) - -

Indirect Iterator Adaptor

- Defined in header boost/iterator_adaptors.hpp - -

The indirect iterator adaptor augments an iterator by applying an - extra dereference inside of operator*(). For example, this - iterator makes it possible to view a container of pointers or - smart-pointers (e.g. std::list<boost::shared_ptr<foo> - >) as if it were a container of the pointed-to type. The following - pseudo-code shows the basic idea of the indirect iterator: - -

-
-// inside a hypothetical indirect_iterator class...
-typedef std::iterator_traits<BaseIterator>::value_type Pointer;
-typedef std::iterator_traits<Pointer>::reference reference;
-
-reference indirect_iterator::operator*() const {
-  return **this->base_iterator;
-}
-
-
- -

Synopsis

- -
-
-namespace boost {
-  template <class BaseIterator,
-            class Value, class Reference, class Category, class Pointer>
-  struct indirect_iterator_generator;
-  
-  template <class BaseIterator,
-            class Value, class Reference, class ConstReference, 
-            class Category, class Pointer, class ConstPointer>
-  struct indirect_iterator_pair_generator;
-
-  template <class BaseIterator>
-  typename indirect_iterator_generator<BaseIterator>::type
-  make_indirect_iterator(BaseIterator base)  
-}
-
-
-
- -

The Indirect Iterator Type - Generator

- The indirect_iterator_generator template is a generator of - indirect iterator types. The main template parameter for this class is the - BaseIterator type that is being wrapped. In most cases the type of - the elements being pointed to can be deduced using - std::iterator_traits, but in some situations the user may want to - override this type, so there are also template parameters that allow a user - to control the value_type, pointer, and - reference types of the resulting iterators. - -
-
-template <class BaseIterator,
-          class Value, class Reference, class Pointer>
-class indirect_iterator_generator
-{
-public:
-  typedef iterator_adaptor<...> type; // the resulting indirect iterator type 
-};
-
-
- -

Example

- This example uses the indirect_iterator_generator to create - indirect iterators which dereference the pointers stored in the - pointers_to_chars array to access the chars in the - characters array. - -
-
-#include <boost/config.hpp>
-#include <vector>
-#include <iostream>
-#include <iterator>
-#include <boost/iterator_adaptors.hpp>
-
-int main(int, char*[])
-{
-  char characters[] = "abcdefg";
-  const int N = sizeof(characters)/sizeof(char) - 1; // -1 since characters has a null char
-  char* pointers_to_chars[N];                        // at the end.
-  for (int i = 0; i < N; ++i)
-    pointers_to_chars[i] = &characters[i];
-  
-  boost::indirect_iterator_generator<char**, char>::type 
-    indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N);
-
-  std::copy(indirect_first, indirect_last, std::ostream_iterator<char>(std::cout, ","));
-  std::cout << std::endl;
-  
-  // to be continued...
-
-
- -

Template Parameters

- - - - - - - - -
Parameter - - Description - -
BaseIterator - - The iterator type being wrapped. The value_type - of the base iterator should itself be dereferenceable. - The return type of the operator* for the - value_type should match the Reference type. - -
Value - - The value_type of the resulting iterator, unless const. If - Value is const X, a conforming compiler makes the - value_type non-const X[1]. Note that if the default - is used for Value, then there must be a valid specialization - of iterator_traits for the value type of the base iterator. -
- Default: std::iterator_traits<
-   std::iterator_traits<BaseIterator>::value_type - >::value_type
[2] - -
Reference - - The reference type of the resulting iterator, and in - particular, the result type of operator*().
- Default: Value& - -
Pointer - - The pointer type of the resulting iterator, and in - particular, the result type of operator->().
- Default: Value* - -
Category - The iterator_category type for the resulting iterator.
- Default: - std::iterator_traits<BaseIterator>::iterator_category - -
- -

Concept Model

- The indirect iterator will model whichever standard iterator - concept category is modeled by the base iterator. Thus, if the - base iterator is a model of Random - Access Iterator then so is the resulting indirect iterator. If - the base iterator models a more restrictive concept, the resulting - indirect iterator will model the same concept [3]. - -

Members

- The indirect iterator type implements the member functions and operators - required of the Random Access - Iterator concept. In addition it has the following constructor: -
-explicit indirect_iterator_generator::type(const BaseIterator& it)
-
-
-
- -
- -

- -

The Indirect Iterator Pair - Generator

- Sometimes a pair of const/non-const pair of iterators is - needed, such as when implementing a container. The - indirect_iterator_pair_generator class makes it more convenient to - create this pair of iterator types. - -
-
-  template <class BaseIterator,
-            class Value, class Reference, class ConstReference, 
-            class Category, class Pointer, class ConstPointer>
-  struct indirect_iterator_pair_generator;
-{
-public:
-  typedef iterator_adaptor<...> iterator;       // the mutable indirect iterator type 
-  typedef iterator_adaptor<...> const_iterator; // the immutable indirect iterator type 
-};
-
-
- -

Example

- -
-
-  // continuing from the last example...
-
-  typedef boost::indirect_iterator_pair_generator<char**,
-    char, char*, char&, const char*, const char&> PairGen;
-
-  char mutable_characters[N];
-  char* pointers_to_mutable_chars[N];
-  for (int i = 0; i < N; ++i)
-    pointers_to_mutable_chars[i] = &mutable_characters[i];
-
-  PairGen::iterator mutable_indirect_first(pointers_to_mutable_chars),
-    mutable_indirect_last(pointers_to_mutable_chars + N);
-  PairGen::const_iterator const_indirect_first(pointers_to_chars),
-    const_indirect_last(pointers_to_chars + N);
-
-  std::transform(const_indirect_first, const_indirect_last,
-     mutable_indirect_first, std::bind1st(std::plus<char>(), 1));
-
-  std::copy(mutable_indirect_first, mutable_indirect_last,
-      std::ostream_iterator<char>(std::cout, ","));
-  std::cout << std::endl;
-  // to be continued...
-
-
- -

The output is: - -

-
-b,c,d,e,f,g,h,
-
-
- -

Template Parameters

- - - - - - - - - - -
Parameter - - Description - -
BaseIterator - - The iterator type being wrapped. The value_type of the - base iterator should itself be dereferenceable. - The return type of the operator* for the - value_type should match the Reference type. - -
Value - - The value_type of the resulting iterators. - If Value is const X, a conforming compiler makes the - value_type non-const X[1]. Note that if the default - is used for Value, then there must be a valid - specialization of iterator_traits for the value type - of the base iterator.
- - Default: std::iterator_traits<
-   std::iterator_traits<BaseIterator>::value_type - >::value_type
[2] - -
Reference - - The reference type of the resulting iterator, and - in particular, the result type of its operator*().
- Default: Value& - -
ConstReference - - The reference type of the resulting - const_iterator, and in particular, the result type of its - operator*().
- Default: const Value& - -
Category - The iterator_category type for the resulting iterator.
- Default: - std::iterator_traits<BaseIterator>::iterator_category - -
Pointer - - The pointer type of the resulting iterator, and - in particular, the result type of its operator->().
- Default: Value* - -
ConstPointer - - The pointer type of the resulting const_iterator, - and in particular, the result type of its operator->().
- Default: const Value* - -
- -

Concept Model

- - The indirect iterators will model whichever standard iterator - concept category is modeled by the base iterator. Thus, if the - base iterator is a model of Random - Access Iterator then so are the resulting indirect - iterators. If the base iterator models a more restrictive concept, - the resulting indirect iterators will model the same concept [3]. - - -

Members

- The resulting iterator and const_iterator types implement - the member functions and operators required of the Random Access - Iterator concept. In addition they support the following constructors: - -
-
-explicit indirect_iterator_pair_generator::iterator(const BaseIterator& it)
-explicit indirect_iterator_pair_generator::const_iterator(const BaseIterator& it)
-
-
-
-
- -
- -

- -

The Indirect Iterator Object - Generator

- The make_indirect_iterator() function provides a more convenient - way to create indirect iterator objects. The function saves the user the - trouble of explicitly writing out the iterator types. - -
-
-template <class BaseIterator>
-typename indirect_iterator_generator<BaseIterator>::type
-make_indirect_iterator(BaseIterator base)  
-
-
- -

Example

- Here we again print the chars from the array characters - by accessing them through the array of pointers pointer_to_chars, - but this time we use the make_indirect_iterator() function which - saves us some typing. - -
-
-  // continuing from the last example...
-
-  std::copy(boost::make_indirect_iterator(pointers_to_chars), 
-      boost::make_indirect_iterator(pointers_to_chars + N),
-      std::ostream_iterator<char>(std::cout, ","));
-  std::cout << std::endl;
-
-  return 0;
-}
-
-
- The output is: - -
-
-a,b,c,d,e,f,g,
-
-
-
- -

Notes

- -

- -

[2] If your compiler does not support partial - specialization and the base iterator or its value_type is a - builtin pointer type, you will not be able to use the default for - Value and will need to specify this type explicitly. - -

[3]There is a caveat to which concept the - indirect iterator can model. If the return type of the - operator* for the base iterator's value type is not a - true reference, then strickly speaking, the indirect iterator can - not be a model of Forward - Iterator or any of the concepts that refine it. In this case - the Category for the indirect iterator should be - specified as std::input_iterator_tag. However, even in - this case, if the base iterator is a random access iterator, the - resulting indirect iterator will still satisfy most of the - requirements for Random - Access Iterator. - -


- -

Revised - 18 Sep 2001 - - -

© Copyright Jeremy Siek and David Abrahams 2001. 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/iterator_adaptors.htm b/iterator_adaptors.htm deleted file mode 100644 index c3d249e..0000000 --- a/iterator_adaptors.htm +++ /dev/null @@ -1,1000 +0,0 @@ - - - - - - - - - - Boost Iterator Adaptor Library - - - - - c++boost.gif (8819 bytes) - -

Boost Iterator Adaptor Library

- -

Introduction

- -

The Iterator Adaptor library allows you transform an arbitrary ``base'' - type into a standard-conforming iterator with the behaviors you choose. - Doing so is especially easy if the ``base'' type is itself an iterator. The - library also supplies several example adaptors which apply - specific useful behaviors to arbitrary base iterators. - -

Backward Compatibility Note

- -

The library's interface has changed since it was first released, breaking - backward compatibility: - -

    - -
  1. Policies classes now operate on instances of the - whole iterator_adaptor object, rather than just operating on the - Base object. This change not only gives the policies class access - to both members of a pair of interacting iterators, but also eliminates the - need for the ugly type<Reference> and - type<Difference> parameters to various policy functions. - -
  2. The Named Template Parameter - interface has been made simpler, easier to use, and compatible with more - compilers. - -
- -

Other Documentation

- -

``Policy Adaptors and the Boost Iterator - Adaptor Library'' is a technical paper describing this library and the - powerful design pattern on which it is based. It was presented at the C++ Template Workshop at OOPSLA - 2001; the slides from the talk are available here. Please note that while the slides - incorporate the minor interface changes described in the previous section, - the paper does not. - -

Table of Contents

- - - -

Dave - Abrahams started the library, applying policy class technique and - handling const/non-const iterator interactions. He also contributed the - indirect_ and reverse_ iterator generators, and expanded - counting_iterator_generator to - cover all incrementable types. He edited most of the documentation, - sometimes heavily.
- Jeremy - Siek contributed the transform - iterator adaptor, the integer-only version of counting_iterator_generator, - the function output iterator - adaptor, and most of the documentation.
- John - Potter contributed the projection_ and filter_ iterator generators and made some - simplifications to the main iterator_adaptor template.
- Jens Maurer - contributed the generator iterator - adaptor.
- Toon Knapen contributed the permutation - iterator adaptor.
- Ronald Garcia - contributed the shared container iterator - adaptor.
- -

Class template - iterator_adaptor

- Implementing standard conforming iterators is a non-trivial task. There are - some fine points such as the interactions between an iterator and its - corresponding const_iterator, and there are myriad operators that should be - implemented but are easily forgotten or mishandled, such as - operator->(). Using iterator_adaptor, you can easily - implement an iterator class, and even more easily extend and adapt existing iterator - types. Moreover, it is easy to make a pair of interoperable const - and non-const iterators. - -

iterator_adaptor is declared like this: -

-template <class Base, class Policies, 
-    class ValueOrNamedParam = typename std::iterator_traits<Base>::value_type,
-    class ReferenceOrNamedParam = ...(see below),
-    class PointerOrNamedParam = ...(see below),
-    class CategoryOrNamedParam = typename std::iterator_traits<Base>::iterator_category,
-    class DistanceOrNamedParam = typename std::iterator_traits<Base>::difference_type>
-struct iterator_adaptor;
-
- -

Template Parameters

- -

Although iterator_adaptor takes seven template parameters, - defaults have been carefully chosen to minimize the number of parameters - you must supply in most cases, especially if Base is an - iterator. - - - - - - - - - - - -
Parameter - - Description - - Requirements - -
Base - - The data type on which the resulting iterator is based. Do - not be misled by the name "Base": this is not a base - class. - - - Assignable, - Default Constructible - -
Policies - - A policy - class that supplies core functionality to the resulting iterator. - - See table below. - -
Value - - The value_type of the resulting iterator, unless const. If - Value is const X the - value_type will be (non-const) X[1]. If the value_type you wish to use is an abstract - base class see note [5].
- Default: - std::iterator_traits<Base>::value_type [2] - -
Reference - - The reference type of the resulting iterator, and in - particular, the result type of operator*().
- Default: If Value is supplied, Value& is - used. Otherwise - std::iterator_traits<Base>::reference is used. [7] - -
ForwardIterators, - BidirectionalIterators, - and RandomAccessIterators - require that Reference is a true reference type (e.g. not a proxy). - -
Pointer - - The pointer type of the resulting iterator, and in - particular, the result type of operator->().
- Default: If Value was not supplied, std::iterator_traits<Base>::pointer. [7] Otherwise, if iterator_category is - input_iterator, then a class yielding - Value* when operator->() is applied. - Otherwise, Value*. - -
value_type* or a - class which yields value_type* when - operator->() is applied. - -
Category - - The iterator_category type for the resulting iterator.
- Default: - std::iterator_traits<Base>::iterator_category - -
One of - std::input_iterator_tag, - std::output_iterator_tag, - std::forward_iterator_tag, - std::bidirectional_iterator_tag, or - std::random_access_iterator_tag. - -
Distance - - The difference_type for the resulting iterator.
- Default: - std::iterator_traits<Base>::difference_type -
A signed integral type - -
NamedParam - - A named template parameter (see below). -
- -

Named Template Parameters

- - With seven template parameters, providing arguments for - iterator_adaptor in the correct order can be challenging. - Also, often times one would like to specify the sixth or seventh - template parameter, but use the defaults for the third through - fifth. As a solution to these problems we provide a mechanism for - naming the last five template parameters, and providing them in - any order through a set of named template parameters. The following - classes are provided for specifying the parameters. Any of these - classes can be used for any of the last five template parameters - of iterator_adaptor. -
-
-template <class Value> struct value_type_is;
-template <class Reference> struct reference_is;
-template <class Pointer> struct pointer_is;
-template <class Distance> struct difference_type_is;
-template <class Category> struct iterator_category_is;
-
-
- - For example, the following adapts foo_iterator to create - an InputIterator - with reference type foo, and whose other traits - are determined according to the defaults described above. - -
-
-typedef iterator_adaptor<foo_iterator, foo_policies,
-  reference_is<foo>, iterator_category_is<std::input_iterator_tag>
-  > MyIterator;
-
-
- - -

The Policies Class

- -

The main task in using iterator_adaptor is creating an - appropriate Policies class. The Policies class will become - the functional heart of the resulting iterator, supplying the core - operations that determine its behavior. The iterator_adaptor - template defines all of the operators required of a Random Access - Iterator by dispatching to a Policies object. Your - Policies class must implement a subset of the core iterator - operations below corresponding to the iterator categories you want it to - support.
-
- - - - - - - - - - - - - -
- Policies Class Requirements
- T: adapted iterator type; x, y: objects of type - T; p: T::policies_type - d: - T::difference_type; i1, i2: - T::base_type -
Expression - - Effects - - Implements Operations - - Required for Iterator Categories - -
p.initialize(b) - - optionally modify base iterator during iterator construction - - constructors - - Input/ Output/ Forward/ Bidirectional/ - Random Access - - -
p.dereference(x) - - returns an element of the iterator's reference type - - *x, x[d] - - -
p.equal(x, y) - - tests the iterator for equality - - i1 == i2, i1 != i2 - -
p.increment(x) - - increments the iterator - - ++x, x++ - -
p.decrement(x) - - decrements the iterator - - --x, x-- - - Bidirectional/ - Random Access - -
p.distance(x, y) - - measures the distance between iterators - - y - x, x < y - - Random - Access - -
p.advance(x, n) - - adds an integer offset to iterators - - -x + d, -d + x, - -
-x += d, -x - d,
-x -= d - -
- -

The library also supplies a "trivial" policy class, - default_iterator_policies, which implements all seven of the core - operations in the usual way. If you wish to create an iterator adaptor that - only changes a few of the base type's behaviors, then you can derive your - new policy class from default_iterator_policies to avoid retyping - the usual behaviors. You should also look at - default_iterator_policies as the ``boilerplate'' for your own - policy classes, defining functions with the same interface. This is the - definition of default_iterator_policies:
-
- -

-
-struct default_iterator_policies
-{
-    // Some of these members were defined static, but Borland got confused
-    // and thought they were non-const. Also, Sun C++ does not like static
-    // function templates.
-
-    template <class Base>
-    void initialize(Base&)
-        { }
-
-    template <class IteratorAdaptor>
-    typename IteratorAdaptor::reference dereference(const IteratorAdaptor& x) const
-        { return *x.base(); }
-
-    template <class IteratorAdaptor>
-    void increment(IteratorAdaptor& x)
-        { ++x.base(); }
-
-    template <class IteratorAdaptor>
-    void decrement(IteratorAdaptor& x)
-        { --x.base(); }
-
-    template <class IteratorAdaptor, class DifferenceType>
-    void advance(IteratorAdaptor& x, DifferenceType n)
-        { x.base() += n; }
-
-    template <class IteratorAdaptor1, class IteratorAdaptor2>
-    typename IteratorAdaptor1::difference_type
-    distance(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const
-        { return y.base() - x.base(); }
-
-    template <class IteratorAdaptor1, class IteratorAdaptor2>
-    bool equal(const IteratorAdaptor1& x, const IteratorAdaptor2& y) const
-        { return x.base() == y.base(); }
-};
-
- -

Template member functions are used throughout - default_iterator_policies so that it can be employed with a wide - range of iterators. If we had used concrete types above, we'd have tied the - usefulness of default_iterator_policies to a particular range of - adapted iterators. If you follow the same pattern with your - Policies classes, you can use them to generate more specialized - adaptors along the lines of those supplied by this library. - -

Additional Members

- In addition to all of the member functions required of a Random Access - Iterator, the iterator_adaptor class template defines the - following members.
-
- - - - - - - -
explicit iterator_adaptor(const Base&, const Policies& = - Policies()) -

- Construct an adapted iterator from a base object and a policies - object. As this constructor is explicit, it does not - provide for implicit conversions from the Base type to - the iterator adaptor. - -
template <class B, class V, class R, class P>
- iterator_adaptor(const - iterator_adaptor<B,Policies,V,R,P,Category,Distance>&)
-

- This constructor allows for conversion from mutable to - constant adapted iterators. See below for more details.
- Requires: B is convertible to Base. - -
const base_type& base() const; -

- Return a const reference to the base object. - -
base_type& base(); -

- Return a reference to the base object. This is to give the policies object - access to the base object. See above for policies - iterator_adaptor interaction.[8] - -
const Policies& policies() const; -

- Return a const reference to the policies object. - -
Policies& policies(); -

- Return a reference to the policies object. -
- -

Example

- -

It is often useful to automatically apply some function to the value - returned by dereferencing an iterator. The transform iterator makes it easy to create - an iterator adaptor which does just that. Here we will show how easy it is - to implement the transform iterator using the iterator_adaptor - template. - -

We want to be able to adapt a range of iterators and functions, so the - policies class will have a template parameter for the function type and it - will have a data member of that type. We know that the function takes one - argument and that we'll need to be able to deduce the result_type - of the function so we can use it for the adapted iterator's - value_type. AdaptableUnaryFunction - is the Concept - that fulfills those requirements. - -

To implement a transform iterator we will only change one of the base - iterator's behaviors, so the transform_iterator_policies class can - inherit the rest from default_iterator_policies. We will define the - dereference() member function, which is used to implement - operator*() of the adapted iterator. The implementation will - dereference the base iterator and apply the function object. The complete - code for transform_iterator_policies is:
-
- -

-template <class AdaptableUnaryFunction>
-struct transform_iterator_policies : public default_iterator_policies
-{
-    transform_iterator_policies() { }
-
-    transform_iterator_policies(const AdaptableUnaryFunction& f)
-        : m_f(f) { }
-    
-    template <class IteratorAdaptor>
-    typename IteratorAdaptor::reference
-    dereference(const IteratorAdaptor& iter) const
-        { return m_f(*iter.base()); }
-
-    AdaptableUnaryFunction m_f;
-};
-
-
- -

The next step is to use the iterator_adaptor template to - construct the transform iterator type. The nicest way to package the - construction of the transform iterator is to create a type generator. - The first template parameter to the generator will be the type of the - function object and the second will be the base iterator type. We use - iterator_adaptor to define the transform iterator type as a nested - typedef inside the transform_iterator_generator class. - Because the function may return by-value, we must limit the - iterator_category to Input Iterator, and - the iterator's reference type cannot be a true reference (the - standard allows this for input iterators), so in this case we can use few - of iterator_adaptor's default template arguments.
-
- - -

-
-template <class AdaptableUnaryFunction, class Iterator>
-struct transform_iterator_generator
-{
-    typedef typename AdaptableUnaryFunction::result_type value_type;
-public:
-    typedef iterator_adaptor<Iterator, 
-        transform_iterator_policies<AdaptableUnaryFunction>,
-        value_type, value_type, value_type*, std::input_iterator_tag>
-      type;
-};
-
-
- -

As a finishing touch, we will create an object generator - for the transform iterator. Our object generator makes it more - convenient to create a transform iterator.
-
- - -

-
-template <class AdaptableUnaryFunction, class Iterator>
-typename transform_iterator_generator<AdaptableUnaryFunction,Iterator>::type
-make_transform_iterator(Iterator base,
-                        const AdaptableUnaryFunction& f = AdaptableUnaryFunction())
-{
-    typedef typename transform_iterator_generator<AdaptableUnaryFunction,
-      Iterator>::type result_t;
-    return result_t(base, f);
-}
-
-
- -

Here is an example that shows how to use a transform iterator to iterate - through a range of numbers, multiplying each of them by 2 and printing the - result to standard output.
-
- - -

-
-#include <functional>
-#include <algorithm>
-#include <iostream>
-#include <boost/iterator_adaptors.hpp>
-
-int main(int, char*[])
-{
-  int x[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
-  const int N = sizeof(x)/sizeof(int);
-  std::cout << "multiplying the array by 2:" << std::endl;
-  std::copy(boost::make_transform_iterator(x, std::bind1st(std::multiplies<int>(), 2)),
-      boost::make_transform_iterator(x + N, std::bind1st(std::multiplies<int>(), 2)),
-      std::ostream_iterator<int>(std::cout, " "));
-  std::cout << std::endl;
-  return 0;
-}
-
- This output is: -
-2 4 6 8 10 12 14 16
-
-
- -

Iterator Interactions

- -

C++ allows const and non-const pointers to interact in - the following intuitive ways: - -

- - Getting user-defined iterators to work together that way is nontrivial (see - here for an example of where - the C++ standard got it wrong), but iterator_adaptor can make it - easy. The rules are as follows: - - - -

Example

- -

The Projection Iterator adaptor is similar to the transform iterator adaptor in that -its operator*() applies some function to the result of -dereferencing the base iterator and then returns the result. The -difference is that the function must return a reference to some -existing object (for example, a data member within the -value_type of the base iterator). - -

-The projection_iterator_pair_generator template - is a special two-type generator for mutable and constant versions of a - projection iterator. It is defined as follows: -

-
-template <class AdaptableUnaryFunction, class Iterator, class ConstIterator>
-struct projection_iterator_pair_generator {
-    typedef typename AdaptableUnaryFunction::result_type value_type;
-    typedef projection_iterator_policies<AdaptableUnaryFunction> policies;
-public:
-    typedef iterator_adaptor<Iterator,policies,value_type> iterator;
-    typedef iterator_adaptor<ConstIterator,policies,value_type,
-        const value_type&,const value_type*> const_iterator;
-};
-
-
- -

It is assumed that the Iterator and ConstIterator arguments are corresponding mutable -and constant iterators.

- -

Challenge

- -

There is an unlimited number of ways the iterator_adaptors - class can be used to create iterators. One interesting exercise would be to - re-implement the iterators of std::list and slist - using iterator_adaptors, where the adapted Iterator types - would be node pointers. - -

Concept Model

- Depending on the Base and Policies template parameters, - an iterator_adaptor can be a Input Iterator, Forward - Iterator, Bidirectional - Iterator, or Random Access - Iterator. - -

Declaration Synopsis

-
-template <class Base, class Policies, 
-    class Value = typename std::iterator_traits<Base>::value_type,
-    class Reference = ...(see below),
-    class Pointer = ...(see below),
-    class Category = typename std::iterator_traits<Base>::iterator_category,
-    class Distance = typename std::iterator_traits<Base>::difference_type
-         >
-struct iterator_adaptor
-{
-    typedef Distance difference_type;
-    typedef typename boost::remove_const<Value>::type value_type;
-    typedef Pointer pointer;
-    typedef Reference reference;
-    typedef Category iterator_category;
-    typedef Base base_type;
-    typedef Policies policies_type;
-
-    iterator_adaptor();
-    explicit iterator_adaptor(const Base&, const Policies& = Policies());
-
-    base_type&       base();
-    const base_type& base() const;
-
-    template <class B, class V, class R, class P>
-    iterator_adaptor(
-        const iterator_adaptor<B,Policies,V,R,P,Category,Distance>&);
-
-    reference operator*() const; [6]
-    operator_arrow_result_type operator->() const; [3]
-    value_type operator[](difference_type n) const; [4], [6]
-
-    iterator_adaptor& operator++();
-    iterator_adaptor& operator++(int);
-    iterator_adaptor& operator--();
-    iterator_adaptor& operator--(int);
-
-    iterator_adaptor& operator+=(difference_type n);
-    iterator_adaptor& operator-=(difference_type n);
-
-    iterator_adaptor& operator-(Distance x) const;
-};
-
-template <class B, class P, class V, class R, class Ptr, 
-    class C, class D1, class D2>
-iterator_adaptor<B,P,V,R,Ptr,C,D1>
-operator+(iterator_adaptor<B,P,V,R,Ptr,C,D1>, D2);
-
-template <class B, class P, class V, class R, class Ptr,
-    class C, class D1, class D2>
-iterator_adaptor<B,P,V,R,P,C,D1>
-operator+(D2, iterator_adaptor<B,P,V,R,Ptr,C,D1> p);
-
-template <class B1, class B2, class P, class V1, class V2,
-    class R1, class R2, class P1, class P2, class C, class D>
-Distance operator-(const iterator_adaptor<B1,P,V1,R1,P1,C,D>&, 
-                   const iterator_adaptor<B2,P,V2,R2,P2,C,D>&);
-
-template <class B1, class B2, class P, class V1, class V2,
-    class R1, class R2, class P1, class P2, class C, class D>
-bool operator==(const iterator_adaptor<B1,P,V1,R1,P1,C,D>&, 
-                const iterator_adaptor<B2,P,V2,R2,P2,C,D>&);
-
-// and similarly for operators !=, <, <=, >=, >
-
- -

Portability

- -

Generally, the iterator adaptors library can be compiled with all compilers - supporting iterator traits and type traits.

- -

Microsoft VC++ is not able to handle iterator adaptors based on a - vector::iterator without specifying all template paramters explicitly. - In case not all template parameters are specified explicitly, the iterator adaptors - library will deduce these types using iterator_traits. But since in VC++ a - vector::iterator is a T*, VC++ can't handle using - iterator_traits due to the lack of partial template specialization.

- - -

Notes

- -

[1] The standard specifies that the value_type - of const iterators to T (e.g. const T*) is - non-const T, while the pointer and - reference types for all Forward Iterators are - const T* and const T&, respectively. Stripping the - const-ness of Value allows you to easily make a constant - iterator by supplying a const type for Value, and allowing - the defaults for the Pointer and Reference parameters to - take effect. Although compilers that don't support partial specialization - won't strip const for you, having a const value_type is - often harmless in practice. - -

[2] If your compiler does not support partial - specialization and the base iterator is a builtin pointer type, you - will not be able to use the default for Value and will have to - specify this type explicitly. - -

[3] The result type for the operator->() - depends on the category and value type of the iterator and is somewhat - complicated to describe. But be assured, it works in a stardard conforming - fashion, providing access to members of the objects pointed to by the - iterator. - -

[4] The result type of operator[]() is - value_type instead of reference as might be expected. - There are two reasons for this choice. First, the C++ standard only - requires that the return type of an arbitrary Random Access - Iterator's operator[]be ``convertible to T'' (Table 76), so - when adapting an arbitrary base iterator we may not have a reference to - return. Second, and more importantly, for certain kinds of iterators, - returning a reference could cause serious memory problems due to the - reference being bound to a temporary object whose lifetime ends inside of - the operator[]. - -

[5] - The value_type of an iterator may not be - an abstract base class, however many common uses of iterators - never need the value_type, only the reference type. - If you wish to create such an iterator adaptor, use a dummy - type such as char for the Value parameter, - and use a reference to your abstract base class for - the Reference parameter. Note that such an iterator - does not fulfill the C++ standards requirements for a - - Forward Iterator, so you will need to use a less restrictive - iterator category such as std::input_iterator_tag. - -

[6] - There is a common misconception that an iterator should have two - versions of operator* and of operator[], one - version that is a const member function and one version - that is non-const. Perhaps the source of this - misconception is that containers typically have const and - non-const versions of many of their member functions. Iterators, - however, are different. A particular iterator type can be either - mutable or constant (but not both). One can assign - to and change the object pointed to by a mutable iterator whereas a - constant iterator returns constant objects when dereferenced. Whether - the iterator object itself is const has nothing to do with - whether the iterator is mutable or constant. This is analogous to - the way built-in pointer types behave. For example, one can - modify objects pointed to by a const pointer -

-    int* const x = new int;
-    int i = 3;
-    *x = i;
-
- but one cannot modify objects pointed to by a pointer - to const -
-    int const* x = new int;
-    int i = 3;
-    *x = i;
-
- -

[7] - If you are using a compiler that does not have a version of - std::iterator_traits that works for pointers (i.e., if your - compiler does not support partial specialization) then if the - Base type is a const pointer, then the correct defaults - for the reference and pointer types can not be - deduced. You must specify these types explicitly. -

[8] - Exposing the base object might be considered as being dangerous. - A possible fix would require compiler support for template friends. - As this is not widely available today, the base object remains exposed for now. - -


- -

Revised - 30 Nov 2001 - - -

© Copyright Dave Abrahams and Jeremy Siek 2001. 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/iterator_adaptors.pdf b/iterator_adaptors.pdf deleted file mode 100644 index 6ae01b0..0000000 Binary files a/iterator_adaptors.pdf and /dev/null differ diff --git a/iterator_adaptors.ppt b/iterator_adaptors.ppt deleted file mode 100644 index b90c0aa..0000000 Binary files a/iterator_adaptors.ppt and /dev/null differ diff --git a/permutation_iterator.htm b/permutation_iterator.htm deleted file mode 100644 index ea68387..0000000 --- a/permutation_iterator.htm +++ /dev/null @@ -1,177 +0,0 @@ - - - - -Permutation Iterator Adaptor Documentation - - - - -

Permutation Iterator Adaptor

-

Defined in header boost/permutation_iterator.hpp

-

The permutation iterator adaptor provides an iterator to a permutation of a given range. -(see definition of permutation). -The adaptor takes two arguments -

- -

Note that the permutation iterator is not limited to strict permutations of the given range V. -The distance between begin and end of the reindexing iterators is allowed to be smaller compared to the -size of the range V, in which case the permutation iterator only provides a permutation of a subrange of V. -The indexes neither need to be unique. In this same context, it must be noted that the past the end permutation iterator is -completely defined by means of the past-the-end iterator to the indices

- -

Synopsis

- -
-
-namespace boost {
-  template <class IndexIterator>
-  class permutation_iterator_policies;
-
-  template <class ElementIterator, class IndexIterator>
-  class permutation_iterator_generator;
-
-  template <class ElementIterator, class IndexIterator>
-  typename permutation_iterator_generator<ElementIterator, IndexIterator>::type
-  make_permutation_iterator(ElementIterator& base, IndexIterator& indexing);
-}
-
-
- - -

The Permutation Iterator Generator Class Template

- -

The permutation_iterator_generator is a helper class whose purpose -is to construct a permutation iterator type. This class has -two template arguments, the first being the iterator type over the range V, the -second being the type of the iterator over the indices. - -

-
-template <class ElementIterator, class IndexIterator>
-class permutation_iterator_generator
-{
-public:
-  typedef iterator_adaptor<...> type; // the resulting permutation iterator type 
-}
-
-
- - -

Template Parameters

- - - - - - - - - - - - - - - -
ParameterDescription
ElementIteratorThe iterator over the elements to be permuted. This type must be a model -of RandomAccessIterator
IndexIteratorThe iterator over the new indexing scheme. This type must at least be a model -of ForwardIterator. -The IndexIterator::value_type must be convertible to the -ElementIterator::difference_type.
- -

Concept Model

-The permutation iterator is always a model of the same concept as the IndexIterator. - -

Members

-The permutation iterator implements the member functions -and operators required for the -Random Access Iterator -concept. However, the permutation iterator can only meet the complexity guarantees -of the same concept as the IndexIterator. Thus for instance, although the permutation -iterator provides operator+=(distance), this operation will take linear time -in case the IndexIterator is a model of ForwardIterator instead of amortized constant time. - -
- -

The Permutation Iterator Object Generator

- -The make_permutation_iterator() function provides a -convenient way to create permutation iterator objects. The function -saves the user the trouble of explicitly writing out the iterator -types. - -
-
-template <class ElementIterator, class IndexIterator >
-typename permutation_iterator_generator<ElementIterator, IndexIterator>::type
-make_permutation_iterator(ElementIterator& base, IndexIterator& indices);
-
-
- -

Example

-
-
-  using namespace boost;
-  int i = 0;
-
-  typedef std::vector< int > element_range_type;
-  typedef std::list< int > index_type;
-
-  static const int element_range_size = 10;
-  static const int index_size = 4;
-
-  element_range_type elements( element_range_size );
-  for(element_range_type::iterator el_it = elements.begin() ; el_it != elements.end() ; ++el_it) *el_it = std::distance(elements.begin(), el_it);
-
-  index_type indices( index_size );
-  for(index_type::iterator i_it = indices.begin() ; i_it != indices.end() ; ++i_it ) *i_it = element_range_size - index_size + std::distance(indices.begin(), i_it);
-  std::reverse( indices.begin(), indices.end() );
-
-  typedef permutation_iterator_generator< element_range_type::iterator, index_type::iterator >::type permutation_type;
-  permutation_type begin = make_permutation_iterator( elements.begin(), indices.begin() );
-  permutation_type it = begin;
-  permutation_type end = make_permutation_iterator( elements.begin(), indices.end() );
-
-  std::cout << "The original range is : ";
-  std::copy( elements.begin(), elements.end(), std::ostream_iterator< int >( std::cout, " " ) );
-  std::cout << "\n";
-
-  std::cout << "The reindexing scheme is : ";
-  std::copy( indices.begin(), indices.end(), std::ostream_iterator< int >( std::cout, " " ) );
-  std::cout << "\n";
-
-  std::cout << "The permutated range is : ";
-  std::copy( begin, end, std::ostream_iterator< int >( std::cout, " " ) );
-  std::cout << "\n";
-
-  std::cout << "Elements at even indices in the permutation : ";
-  it = begin;
-  for(i = 0; i < index_size / 2 ; ++i, it+=2 ) std::cout << *it << " ";
-  std::cout << "\n";
-
-  std::cout << "Permutation backwards : ";
-  it = begin + (index_size);
-  assert( it != begin );
-  for( ; it-- != begin ; ) std::cout << *it << " ";
-  std::cout << "\n";
-
-  std::cout << "Iterate backward with stride 2 : ";
-  it = begin + (index_size - 1);
-  for(i = 0 ; i < index_size / 2 ; ++i, it-=2 ) std::cout << *it << " ";
-  std::cout << "\n";
-
-
- -



-Thanks: The permutation iterator is only a small addition to the superb iterator adaptors -library of David Abrahams and Jeremy Siek. -

- -Copyright 2001 Toon Knapen. - - - diff --git a/projection_iterator.htm b/projection_iterator.htm deleted file mode 100644 index e6b0df3..0000000 --- a/projection_iterator.htm +++ /dev/null @@ -1,391 +0,0 @@ - - - - - - -Projection Iterator Adaptor Documentation - - - - -c++boost.gif (8819 bytes) - -

Projection Iterator Adaptor

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

-The projection iterator adaptor is similar to the transform iterator adaptor in that -its operator*() applies some function to the result of -dereferencing the base iterator and then returns the result. The -difference is that the function must return a reference to some -existing object (for example, a data member within the -value_type of the base iterator). The following -pseudo-code gives the basic idea. The data member p is -the function object. - -

-  reference projection_iterator::operator*() const {
-    return this->p(*this->base_iterator);
-  }
-
- -

Synopsis

- -
-namespace boost {
-  template <class AdaptableUnaryFunction, class BaseIterator>
-  struct projection_iterator_generator;
-  
-  template <class AdaptableUnaryFunction, 
-            class BaseIterator, class ConstBaseIterator>
-  struct projection_iterator_pair_generator;
-
-  template <class AdaptableUnaryFunction, class BaseIterator>
-  typename projection_iterator_generator<AdaptableUnaryFunction, BaseIterator>::type
-  make_projection_iterator(BaseIterator base,
-			   const AdaptableUnaryFunction& p = AdaptableUnaryFunction())
-
-  template <class AdaptableUnaryFunction, class ConstBaseIterator>
-  typename projection_iterator_generator<AdaptableUnaryFunction, ConstBaseIterator>::type
-  make_const_projection_iterator(ConstBaseIterator base,
-                                 const AdaptableUnaryFunction& p = AdaptableUnaryFunction())  
-}
-
- -
- -

The Projection Iterator Type -Generator

- -The class projection_iterator_generator is a helper class -whose purpose is to construct an projection iterator type. The main -template parameter for this class is the AdaptableUnaryFunction -function object type and the BaseIterator type that is being -wrapped. - -
-template <class AdaptableUnaryFunction, class BaseIterator>
-class projection_iterator_generator
-{
-public:
-  typedef iterator_adaptor<...> type; // the resulting projection iterator type 
-};
-
- -

Example

- -In the following example we have a list of personnel records. Each -record has an employee's name and ID number. We want to be able to -traverse through the list accessing either the name or the ID numbers -of the employees using the projection iterator so we create the -function object classes select_name and -select_ID. We then use the -projection_iterator_generator class to create a projection -iterator and use it to print out the names of the employees. - -
-#include <boost/config.hpp>
-#include <list>
-#include <iostream>
-#include <iterator>
-#include <algorithm>
-#include <string>
-#include <boost/iterator_adaptors.hpp>
-
-struct personnel_record {
-  personnel_record(std::string n, int id) : m_name(n), m_ID(id) { }
-  std::string m_name;
-  int m_ID;
-};
-
-struct select_name {
-  typedef personnel_record argument_type;
-  typedef std::string result_type;
-  const std::string& operator()(const personnel_record& r) const {
-    return r.m_name;
-  }
-  std::string& operator()(personnel_record& r) const {
-    return r.m_name;
-  }
-};
-
-struct select_ID {
-  typedef personnel_record argument_type;
-  typedef int result_type;
-  const int& operator()(const personnel_record& r) const {
-    return r.m_ID;
-  }
-  int& operator()(personnel_record& r) const {
-    return r.m_ID;
-  }
-};
-
-int main(int, char*[])
-{
-  std::list<personnel_record> personnel_list;
-
-  personnel_list.push_back(personnel_record("Barney", 13423));
-  personnel_list.push_back(personnel_record("Fred", 12343));
-  personnel_list.push_back(personnel_record("Wilma", 62454));
-  personnel_list.push_back(personnel_record("Betty", 20490));
-
-  // Example of using projection_iterator_generator
-  // to print out the names in the personnel list.
-
-  boost::projection_iterator_generator<select_name,
-    std::list<personnel_record>::iterator>::type
-    personnel_first(personnel_list.begin()),
-    personnel_last(personnel_list.end());
-
-  std::copy(personnel_first, personnel_last,
-            std::ostream_iterator<std::string>(std::cout, "\n"));
-  std::cout << std::endl;
-
-  // to be continued...
-
-The output for this part is: -
-Barney
-Fred
-Wilma
-Betty
-
- -

Template Parameters

- - - - - - - - - - - - - - - - - -
ParameterDescription
AdaptableUnaryFunctionThe type of the function object. The argument_type of the -function must match the value type of the base iterator. The function -should return a reference to the function's result_type. -The result_type will be the resulting iterator's value_type. -
BaseIteratorThe iterator type being wrapped.
- -

Model of

- -If the base iterator is a model of Random -Access Iterator then so is the resulting projection iterator. If -the base iterator supports less functionality than this the resulting -projection iterator will also support less functionality. - -

Members

- -The projection iterator type implements the member functions and -operators required of the Random -Access Iterator concept. -In addition it has the following constructor: - -
-projection_iterator_generator::type(const BaseIterator& it,
-                                    const AdaptableUnaryFunction& p = AdaptableUnaryFunction())
-
- -

-


-

- -

The Projection Iterator Pair -Generator

- -Sometimes a mutable/const pair of iterator types is needed, such as -when implementing a container type. The -projection_iterator_pair_generator class makes it more -convenient to create this pair of iterator types. - -
-template <class AdaptableUnaryFunction, class BaseIterator, class ConstBaseIterator>
-class projection_iterator_pair_generator
-{
-public:
-  typedef iterator_adaptor<...> iterator;       // the mutable projection iterator type 
-  typedef iterator_adaptor<...> const_iterator; // the immutable projection iterator type 
-};
-
- -

Example

- -In this part of the example we use the -projection_iterator_pair_generator to create a mutable/const -pair of projection iterators that access the ID numbers of the -personnel. We use the mutable iterator to re-index the ID numbers from -zero. We then use the constant iterator to print the ID numbers out. - -
-  // continuing from the last example...
-
-  typedef boost::projection_iterator_pair_generator<select_ID,
-    std::list<personnel_record>::iterator,
-    std::list<personnel_record>::const_iterator> PairGen;
-
-  PairGen::iterator ID_first(personnel_list.begin()),
-    ID_last(personnel_list.end());
-
-  int new_id = 0;
-  while (ID_first != ID_last) {
-    *ID_first = new_id++;
-    ++ID_first;
-  }
-
-  PairGen::const_iterator const_ID_first(personnel_list.begin()),
-    const_ID_last(personnel_list.end());
-
-  std::copy(const_ID_first, const_ID_last,
-            std::ostream_iterator<int>(std::cout, " "));
-  std::cout << std::endl;
-  std::cout << std::endl;
-  
-  // to be continued...
-
-0 1 2 3 
-
- -

Template Parameters

- - - - - - - - - - - - - - - - - - - - - - - -
ParameterDescription
AdaptableUnaryFunctionThe type of the function object. The argument_type of the -function must match the value type of the base iterator. The function -should return a true reference to the function's result_type. -The result_type will be the resulting iterator's value_type. -
BaseIteratorThe mutable iterator type being wrapped.
ConstBaseIteratorThe constant iterator type being wrapped.
- -

Model of

- -If the base iterator types model the Random -Access Iterator then so do the resulting projection iterator -types. If the base iterators support less functionality the -resulting projection iterator types will also support less -functionality. The resulting iterator type is mutable, and -the resulting const_iterator type is constant. - -

Members

- -The resulting iterator and const_iterator types -implements the member functions and operators required of the Random -Access Iterator concept. In addition they support the following -constructors: - -
-projection_iterator_pair_generator::iterator(const BaseIterator& it,
-                                             const AdaptableUnaryFunction& p = AdaptableUnaryFunction())
- -
-projection_iterator_pair_generator::const_iterator(const BaseIterator& it,
-                                                   const AdaptableUnaryFunction& p = AdaptableUnaryFunction())
-
- -

-


-

- -

The Projection Iterator Object Generators

- -The make_projection_iterator() and -make_const_projection_iterator() functions provide a more -convenient way to create projection iterator objects. The functions -save the user the trouble of explicitly writing out the iterator -types. - -
-template <class AdaptableUnaryFunction, class BaseIterator>
-typename projection_iterator_generator<AdaptableUnaryFunction, BaseIterator>::type
-make_projection_iterator(BaseIterator base,
-			 const AdaptableUnaryFunction& p = AdaptableUnaryFunction())  
-
-template <class AdaptableUnaryFunction, class ConstBaseIterator>
-typename projection_iterator_generator<AdaptableUnaryFunction, ConstBaseIterator>::type
-make_const_projection_iterator(ConstBaseIterator base,
-			       const AdaptableUnaryFunction& p = AdaptableUnaryFunction())  
-
- - -

Example

- -In this part of the example, we again print out the names of the -personnel, but this time we use the -make_const_projection_iterator() function to save some typing. - -
-  // continuing from the last example...
-
-  std::copy
-    (boost::make_const_projection_iterator<select_name>(personnel_list.begin()),
-     boost::make_const_projection_iterator<select_name>(personnel_list.end()),
-     std::ostream_iterator(std::cout, "\n"));
-
-  return 0;
-}
-
-The output is: -
-Barney
-Fred
-Wilma
-Betty
-
- -
-

Revised 19 Aug 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/reverse_iterator.htm b/reverse_iterator.htm deleted file mode 100644 index d20735f..0000000 --- a/reverse_iterator.htm +++ /dev/null @@ -1,331 +0,0 @@ - - - - - - - - - - Reverse Iterator Adaptor Documentation - - - - c++boost.gif (8819 bytes) - -

Reverse Iterator Adaptor

- Defined in header boost/iterator_adaptors.hpp - -

The reverse iterator adaptor flips the direction of a base iterator's - motion. Invoking operator++() moves the base iterator backward and - invoking operator--() moves the base iterator forward. The Boost - reverse iterator adaptor is better to use than the - std::reverse_iterator class in situations where pairs of - mutable/constant iterators are needed (e.g., in containers) because - comparisons and conversions between the mutable and const versions are - implemented correctly. - -

Synopsis

-
-namespace boost {
-  template <class BidirectionalIterator,
-            class Value, class Reference, class Pointer, class Category, class Distance>
-  struct reverse_iterator_generator;
-  
-  template <class BidirectionalIterator>
-  typename reverse_iterator_generator<BidirectionalIterator>::type
-  make_reverse_iterator(BidirectionalIterator base)  
-}
-
-
- -

The Reverse Iterator Type - Generator

- The reverse_iterator_generator template is a generator of - reverse iterator types. The main template parameter for this class is the - base BidirectionalIterator type that is being adapted. In most - cases the associated types of the base iterator can be deduced using - std::iterator_traits, but in some situations the user may want to - override these types, so there are also template parameters for the base - iterator's associated types. - -
-
-template <class BidirectionalIterator,
-          class Value, class Reference, class Pointer, class Category, class Distance>
-class reverse_iterator_generator
-{
-public:
-  typedef iterator_adaptor<...> type; // the resulting reverse iterator type 
-};
-
-
- -

Example

- In this example we sort a sequence of letters and then output the sequence - in descending order using reverse iterators. - -
-
-#include <boost/config.hpp>
-#include <iostream>
-#include <algorithm>
-#include <boost/iterator_adaptors.hpp>
-
-int main(int, char*[])
-{
-  char letters[] = "hello world!";
-  const int N = sizeof(letters)/sizeof(char) - 1;
-  std::cout << "original sequence of letters:\t"
-      << letters << std::endl;
-
-  std::sort(letters, letters + N);
-
-  // Use reverse_iterator_generator to print a sequence
-  // of letters in reverse order.
-  
-  boost::reverse_iterator_generator<char*>::type
-    reverse_letters_first(letters + N),
-    reverse_letters_last(letters);
-
-  std::cout << "letters in descending order:\t";
-  std::copy(reverse_letters_first, reverse_letters_last,
-      std::ostream_iterator<char>(std::cout));
-  std::cout << std::endl;
-
-  // to be continued...
-
-
- The output is: - -
-
-original sequence of letters: hello world!
-letters in descending order:  wroolllhed! 
-
-
- -

Template Parameters

- - - - - - - - - -
Parameter - - Description - -
BidirectionalIterator - - - The iterator type being wrapped. - -
Value - - The value-type of the base iterator and the resulting reverse - iterator.
- Default:std::iterator_traits<BidirectionalIterator>::value_type - - -
Reference - - The reference type of the resulting iterator, and in - particular, the result type of operator*().
- Default: If Value is supplied, Value& is - used. Otherwise - std::iterator_traits<BidirectionalIterator>::reference - is used. - -
Pointer - - The pointer type of the resulting iterator, and in - particular, the result type of operator->().
- Default: If Value was supplied, then Value*, - otherwise - std::iterator_traits<BidirectionalIterator>::pointer. - -
Category - - The iterator_category type for the resulting iterator.
- Default: - std::iterator_traits<BidirectionalIterator>::iterator_category - - -
Distance - - The difference_type for the resulting iterator.
- Default: - std::iterator_traits<BidirectionalIterator&gt::difference_type - -
- -

Concept Model

- The indirect iterator will model whichever standard iterator concept - category is modeled by the base iterator. Thus, if the base iterator is - a model of Random Access - Iterator then so is the resulting indirect iterator. If the base - iterator models a more restrictive concept, the resulting indirect iterator - will model the same concept. The base iterator must be at least a Bidirectional - Iterator - -

Members

- The reverse iterator type implements the member functions and operators - required of the Random Access - Iterator concept. In addition it has the following constructor: - -
-
-reverse_iterator_generator::type(const BidirectionalIterator& it)
-
-
- - -
-
- -
- -

- -

The Reverse Iterator Object - Generator

- The make_reverse_iterator() function provides a more convenient - way to create reverse iterator objects. The function saves the user the - trouble of explicitly writing out the iterator types. - -
-
-template <class BidirectionalIterator>
-typename reverse_iterator_generator<BidirectionalIterator>::type
-make_reverse_iterator(BidirectionalIterator base);
-
-
- -

Example

- In this part of the example we use make_reverse_iterator() to - print the sequence of letters in reverse-reverse order, which is the - original order. - -
-
-  // continuing from the previous example...
-
-  std::cout << "letters in ascending order:\t";
-  std::copy(boost::make_reverse_iterator(reverse_letters_last),
-      boost::make_reverse_iterator(reverse_letters_first),
-      std::ostream_iterator<char>(std::cout));
-  std::cout << std::endl;
-
-  return 0;
-}
-
-
- The output is: - -
-
-letters in ascending order:  !dehllloorw
-
-
-
- -

Constant/Mutable Iterator Interactions

- -

One failing of the standard reverse_iterator - adaptor is that it doesn't properly support interactions between adapted - const and non-const iterators. For example: -

-
-#include <vector>
-
-template <class T> void convert(T x) {}
-
-// Test interactions of a matched pair of random access iterators
-template <class Iterator, class ConstIterator>
-void test_interactions(Iterator i, ConstIterator ci)
-{
-  bool eq = i == ci;               // comparisons
-  bool ne = i != ci;            
-  bool lt = i < ci;
-  bool le = i <= ci;
-  bool gt = i > ci;
-  bool ge = i >= ci;
-  std::size_t distance = i - ci;   // difference
-  ci = i;                          // assignment
-  ConstIterator ci2(i);            // construction
-  convert<ConstIterator>(i);       // implicit conversion
-}
-
-void f()
-{
-  typedef std::vector<int> vec;
-  vec v;
-  const vec& cv;
-
-  test_interactions(v.begin(), cv.begin());   // OK
-  test_interactions(v.rbegin(), cv.rbegin()); // ERRORS ON EVERY TEST!!
-
-
-Reverse iterators created with boost::reverse_iterator_generator don't have this problem, though: -
-
-  typedef boost::reverse_iterator_generator<vec::iterator>::type ri;
-  typedef boost::reverse_iterator_generator<vec::const_iterator>::type cri;
-  test_interactions(ri(v.begin()), cri(cv.begin()));   // OK!!
-
-
-Or, more simply, -
-
-  test_interactions(
-    boost::make_reverse_iterator(v.begin()), 
-    boost::make_reverse_iterator(cv.begin()));   // OK!!
-}
-
-
- -

If you are wondering why there is no -reverse_iterator_pair_generator in the manner of projection_iterator_pair_generator, -the answer is simple: we tried it, but found that in practice it took -more typing to use reverse_iterator_pair_generator than to -simply use reverse_iterator_generator twice!

- -


- - -

Revised - 19 Aug 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/transform_iterator.htm b/transform_iterator.htm deleted file mode 100644 index 4b3dce3..0000000 --- a/transform_iterator.htm +++ /dev/null @@ -1,223 +0,0 @@ - - - - - - -Transform Iterator Adaptor Documentation - - - - -c++boost.gif (8819 bytes) - -

Transform Iterator Adaptor

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

-The transform iterator adaptor augments an iterator by applying some -function object to the result of dereferencing the iterator. In other -words, the operator* of the transform iterator first -dereferences the base iterator, passes the result of this to the -function object, and then returns the result. The following -pseudo-code shows the basic idea: - -

-  value_type transform_iterator::operator*() const {
-    return this->f(*this->base_iterator);
-  }
-
- -All of the other operators of the transform iterator behave in the -same fashion as those of the base iterator. - - -

Synopsis

- -
-namespace boost {
-  template <class AdaptableUnaryFunction, class BaseIterator>
-  class transform_iterator_generator;
-
-  template <class AdaptableUnaryFunction, class BaseIterator>
-  typename transform_iterator_generator<AdaptableUnaryFunction,Iterator>::type
-  make_transform_iterator(BaseIterator base, const AdaptableUnaryFunction& f = AdaptableUnaryFunction());
-}
-
- -
- -

The Transform Iterator Type -Generator

- -The class transform_iterator_generator is a helper class whose -purpose is to construct a transform iterator type. The template -parameters for this class are the AdaptableUnaryFunction function object -type and the BaseIterator type that is being wrapped. - -
-template <class AdaptableUnaryFunction, class Iterator>
-class transform_iterator_generator
-{
-public:
-    typedef iterator_adaptor<...> type;
-};
-
- -

Example

- -

-The following is an example of how to use the -transform_iterator_generator class to iterate through a range -of numbers, multiplying each of them by 2 when they are dereferenced. -The boost::binder1st class is used instead of the standard -one because tranform iterator requires the function object to be -Default Constructible. - -

-

-#include <functional>
-#include <iostream>
-#include <boost/iterator_adaptors.hpp>
-
-// definition of class boost::binder1st and function boost::bind1st() ...
-
-int
-main(int, char*[])
-{
-  int x[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
-
-  typedef boost::binder1st< std::multiplies<int> > Function;
-  typedef boost::transform_iterator_generator<Function, int*>::type doubling_iterator;
-
-  doubling_iterator i(x, boost::bind1st(std::multiplies<int>(), 2)),
-    i_end(x + sizeof(x)/sizeof(int), boost::bind1st(std::multiplies<int>(), 2));
-
-  std::cout << "multiplying the array by 2:" << std::endl;
-  while (i != i_end)
-    std::cout << *i++ << " ";
-  std::cout << std::endl;
-
-  // to be continued...
-
-The output from this part is: -
-2 4 6 8 10 12 14 16
-
- -

Template Parameters

- - - - - - - - - - - - - - - - -
ParameterDescription
AdaptableUnaryFunctionThe function object that transforms each element in the iterator -range. The argument_type of the function object must match -the value type of the base iterator. The result_type of the -function object will be the resulting iterator's -value_type. If you want the resulting iterator to behave as -an iterator, the result of the function should be solely a function of -its argument. Also, the function object must be Default -Constructible (which many of the standard function objects are not).
BaseIteratorThe iterator type being wrapped. This type must at least be a model - of the InputIterator concept.
- -

Model of

- -The transform iterator adaptor (the type -transform_iterator_generator<...>::type) is a model of Input Iterator[1]. - - -

Members

- -The transform iterator type implements the member functions and -operators required of the Random Access Iterator -concept, except that the reference type is the same as the value_type -so operator*() returns by-value. In addition it has the following constructor: - -
-transform_iterator_generator::type(const BaseIterator& it,
-                                   const AdaptableUnaryFunction& f = AdaptableUnaryFunction())
-
- -

-


-

- - -

The Transform Iterator Object Generator

- -
-template <class AdaptableUnaryFunction, class BaseIterator>
-typename transform_iterator_generator<AdaptableUnaryFunction,BaseIterator>::type
-make_transform_iterator(BaseIterator base,
-                        const AdaptableUnaryFunction& f = AdaptableUnaryFunction());
-
- -This function provides a convenient way to create transform iterators. - -

Example

- -Continuing from the previous example, we use the make_transform_iterator() -function to add four to each element of the array. - -
-  std::cout << "adding 4 to each element in the array:" << std::endl;
-
-  std::copy(boost::make_transform_iterator(x, boost::bind1st(std::plus(), 4)),
-	    boost::make_transform_iterator(x + N, boost::bind1st(std::plus(), 4)),
-	    std::ostream_iterator(std::cout, " "));
-  std::cout << std::endl;
-
-  return 0;
-}
-
-The output from this part is: -
-5 6 7 8 9 10 11 12
-
- -

Notes

- - -[1] If the base iterator is a model of Random Access Iterator -then the transform iterator will also suppport most of the -functionality required by the Random Access Iterator concept. However, a -transform iterator can never completely satisfy the requirements for -Forward Iterator -(or of any concepts that refine Forward Iterator, which includes -Random Access Iterator and Bidirectional Iterator) since the operator* of the transform -iterator always returns by-value. - - - -
-

Revised 19 Aug 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.

- - - -