[Boost] boost: range (range) Introduction

Source: Internet
Author: User

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 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.