chapter 3 About boost::range

Concept

  1. Range:

    • A concept similiar with STL container concept to fewer requirements.
    • Has 2 iterators that refer to the beginning and end of a group of elements that you can iterate over.
  2. Range adaptor:

    • A class that wraps an existing Range to provide a new Range with different behaviour.
    • Provides iterators for accessing a half-open range [first,one_past_last) of elements.
    • Wraps the underlying iterators with new special iterators since the behaviour of Ranges is determined by their associated iterators.
  3. Goal:

    • Range does not necessarily own the elements that can be accessed through it. (light)
    • Range object must be passed by reference in generic code. (performance)
  4. Category:

    • Includes 4 types: Single pass range, Forward range, Bidirectional rangeand Random access range.
    • RandomAccessRng > BidirectionalRng > ForwardRng > SinglePassRng.

Adapter algorithm (Boost.Range):

  • Expect a range as a first parameter, a container like std::vector can be passed directly.
  • adjacent_filtered, copied, filtered, indexed, indirected, map_keys, map_values, replaced, replaced_if, reversed, sliced, strided, type_erased, tokenized, transformed and uniqued.

    • boost::map_values (Single Pass Range)

      #include <boost/range/adaptor/filtered.hpp>
      #include <boost/range/algorithm/copy.hpp>
      #include <boost/assign.hpp>
      #include <iterator>
      #include <iostream>
      #include <vector>
      
      using namespace boost::assign;
      using namespace boost::adaptors;
      int main(int argc, const char* argv[])
      {
          std::map<int,int> input;
          for (int i = 0; i < 10; ++i)
              input.insert(std::make_pair(i, i * 10));
      
          boost::copy(
              input | map_values,
              std::ostream_iterator<int>(std::cout, ","));
      
          return 0;
      }
      Output: 0,10,20,30,40,50,60,70,80,90,
      
    • boost::filtered (Forward Range)
      struct is_even {
          bool operator()( int x ) const { return x % 2 == 0; }
      };
      int main()
      {
          std::vector<int> input;
          input += 1,2,3,4,5,6,7,8,9;
          boost::copy(
              input | filtered(is_even()),
              std::ostream_iterator<int>(std::cout, ","));
          return 0;
      }
      Output: 2,4,6,8,
      
    • boost::reversed (Bidirectional Range)
      int main()
      {
          std::vector<int> input;
          input += 1,2,3,4,5,6,7,8,9;
          boost::copy(
              input | reversed,
              std::ostream_iterator<int>(std::cout, ","));
          return 0;
      }
      Output: 9,8,7,6,5,4,3,2,1,
      
    • boost::copied (Random Access Range)
      int main()
      {
        std::vector<int> input;
        input += 1,2,3,4,5,6,7,8,9,10;
        boost::copy(
            input | copied(1, 5),
            std::ostream_iterator<int>(std::cout, ","));
        return 0;
      }
      Output: 2,3,4,5,
      

results matching ""

    No results matching ""