Section 7.6.  Exercises
Team LiB
Previous Section Next Section

7.6. Exercises

7-0.

Write a test program that exercises our zip_view implementation. Try to arrange your program so that it will only compile if the tests succeed.

7-1.

Our implementation of zip_iterator uses transform to generate its nested ::type, but the one in MPL uses transform_view instead. What advantage does the MPL approach have?

7-2.

Modify zip_iterator so that its ::iterator_category reflects the least-refined concept modeled by any of its underlying iterators. Extend the iterator implementation to satisfy all potential requirements of the computed category.

7-3.

Use mpl::joint_view to implement a rotate_view sequence view, presenting a shifted and wrapped view onto the original sequence:



    typedef mpl::vector_c<int,5,6,7,8,9,0,1,2,3,4> v;


    typedef rotate_view<


        v


      , mpl::advance_c<mpl::begin<v>::type,5>::type


    > view;


    BOOST_STATIC_ASSERT(( mpl::equal<


        view


      , mpl::range_c<int,0,10>


    >::value ));



7-4.

Design and implement an iterator adaptor that adapts any Random Access Iterator by presenting the elements it traverses in an order determined by a sequence of nonnegative integer indices. Make your permutation_iterator a forward iterator.

7-5.

Change the permutation iterator from exercise 7-4 so its traversal category is determined by the category of the sequence of indices.

7-6.

Implement a permutation_view using your permutation iterator adaptor, so that:



    permutation_view<


        mpl::list_c<int,2,1,3,0,2>     // indices


      , mpl::vector_c<int,11,22,33,44> // elements


    >



yields sequence [33,22,44,11,33]

7-7.

Design and implement a reverse iterator adaptor with semantics analogous to those of std::reverse_iterator. Make its category the same as the category of the underlying iterator. Use the resulting iterator to implement a reverse_view template.

7-8.

Implement a crossproduct_view template that adapts two original sequences by presenting all possible pairs of their elements in a right cross product order.

    Team LiB
    Previous Section Next Section