1. Concept
The range concept is similar to the container concept in STL. An interval provides an iterator that can access elements in [first, one_past_last) of a semi-open interval, and provides information about the number of elements in the interval.
1.1 Purpose
The purpose of introducing the range concept is to include many types similar to containers and simplified algorithms for these types.
1.2 type used
Similar to standard containers
STD: pair <iterator, iterator>
Built-in array
2. Example
Constructor
Void test_range_construct_string () {typedef STD: String: iteratoriterator; typedef STD: String: pair; typedef boost: iterator_range <iterator> irange; typedef boost :: iterator_range <const_iterator> cirange; STD: String STR = "Hello World"; const STD: String CSTR = "const world"; // 1. basic Construction Method: boost: iterator_range <STD: String: iterator> ir (STR); Boost: iterator_range <STD: String: const_iterator> CIR (STR ); // 2. use make_iterator_range (several overload functions) irange r = boost: make_iterator_range (STR); r = boost: make_iterator_range (Str. begin (), str. end (); cirange r2 = boost: make_iterator_range (CSTR); r2 = boost: make_iterator_range (CSTR. begin (), CSTR. end (); r2 = boost: make_iterator_range (STR); Assert (r = Str); Assert (R. size () = 11); irange R3 = boost: make_iterator_range (STR, 1,-1); Assert (boost: as_literal ("ello worl ") = R3); irange r4 = boost: make_iterator_range (R3,-1, 1); // This can also be understood as copying to construct assert (STR = R4); STD:: cout <R4 <STD: Endl; irange R5 = boost: make_iterator_range (Str. begin (), str. begin () + 5); Assert (R5 = boost: as_literal ("hello "));}
Type Change
Void test_range_type () {using namespace boost; // array const int size = 9; typedef int array_t [size]; const array_t CA = {1, 2, 3, 4, 5, 6, 7, 8, 10}; Assert (is_same <range_iterator <array_t>: type, int * >:: value )); assert (is_same <range_value <array_t >:: type, int >:: value); Assert (is_same <range_difference <array_t >:: type, STD: ptrdiff_t>:: Value); Assert (is_same <range_size <array_t >:: type, STD: size_t >:: value); Assert (is_same <range_const_iterator <array_t> :: type, const int *>: Value); Assert (begin (CA) = Ca); Assert (end (CA) = Ca + size (CA )); assert (empty (CA) = false );}