Boost: array Introduction

Source: Internet
Author: User

This article introduces the container that supports static arrays in C ++: boost. array.

Boost. array is likely to appear in the Next Generation of standard libraries.

 

1. Why do we need containers with fixed-size arrays?

First of all, fixed-size arrays are still very common. Although STL provides vector, vector, as a dynamically scalable array, has a higher overhead than static arrays, this is intolerable to some people. C ++ also needs to provide a fixed size array container, of course, the performance can be comparable to ordinary arrays.

Boost. array is generated based on this requirement.

 

Boost. array meets most but not all of the "reversable container" requirements. Array is not an STL reversible container because:

  • No constructor is provided.
  • The element may have an uncertain initial value.
  • swap() Does not have constant complexity.
  • size() The type based on the second template parameter is always constant.
  • Containers do not support splitters.

It does not meet the "sequence" requirement (see the C ++ standard 23.1.1, [Lib. sequence. reqmts]), except for the following:

  • Providedfront() Andback().
  • Providedoperator[]Andat().
2. header file and related member function declaration: Reference

Header <boost/array. HPP>

array

  1. template<typename U> array& operator=(const array<U, N>& other);
Class template array (class template array)Public constructor/copy/destructor

arrayIterator support

  1. iterator begin();const_iterator begin() const;

    Return Value:

    Iterator located in the first element

    Throw:

    Do not throw an exception
  2. iterator end();const_iterator end() const;

    Return Value:

    Iterator located behind the last element

    Throw:

    Do not throw an exception

array
Support for reverse iterators
  1. reverse_iterator rbegin();const_reverse_iterator rbegin() const;

    Return Value:

    The reverse iterator located in the first element of the reverse Iteration
  2. reverse_iterator rend();const_reverse_iterator rend() const;

    Return Value:

    The reverse iterator located after the last element of the reverse Iteration

arrayCapacity
  1. size_type size();

    Return Value:

    N
  2. bool empty();

    Return Value:

    N==0

    Throw:

    Do not throw an exception
  3. size_type max_size();

    Return Value:

    N

    Throw:

    Do not throw an exception

arrayElement access
  1. reference operator[](size_type i);const_reference operator[](size_type i) const;

    Requires:

    i < N

    Return Value:

    Index isiElement

    Throw:

    No exception is thrown.
  2. reference at(size_type i);const_reference at(size_type i) const;

    Return Value:

    Index isiElement

    Throw:

    std::range_errorIfi >= N
  3. reference front();const_reference front() const;

    Requirements:

    N > 0

    Return Value:

    First element

    Throw:

    Do not throw an exception
  4. reference back();const_reference back() const;

    Requirements:

    N > 0

    Return Value:

    Last Element

    Throw:

    Do not throw an exception
  5. const T* data() const;

    Return Value:

    elems

    Throw:

    Do not throw an exception
  6. T* c_array();

    Return Value:

    elems

    Throw:

    Do not throw an exception
arrayModifier

  1. void swap(array<T, N>& other);

    Effect:

    std::swap_ranges(begin(), end(), other.begin())

    Complexity:

    Based onN
    Linear growth
  2. void assign(const T& value);

    Effect:

    std::fill_n(begin(), N,
    value)
arraySpecial Algorithms

  1. template<typename T, std::size_t N> void swap(array<T, N>& x, array<T, N>& y);

    Effect:

    x.swap(y)

    Throw:

    No exception is thrown.
It can be seen that boost. array provides common interfaces with STL containers. Therefore, it is easy to use. It is worth mentioning that boost does not provide custom constructor and copy constructor. However, boost. array can be initialized as follows:


# Include <boost/array. HPP>


# Include <iostream>


Using namespace STD;

Using namespace boost;

Int
Main ()

{

Array <int, 6> A = {1, 2, 4, 5, 6 };

// The ACCESS format of common arrays is consistent

For (size_t I = 0; I <A. Size (); I ++)

Cout <A [I] <"";

Cout <Endl;

 

// Iterator access

Array <int, 6 >:: iterator itr = A. Begin ();

For (; itr! = A. End (); ++ itr)

Cout <* itr <"";

Cout <Endl;

// Supports ()

Cout <A. At (5) <Endl;

   


Return 0;

}

I believe that boost. array is definitely your choice. Welcome to your criticism and suggestions, please contact bicheng.gui@gmail.com if you have any questions. The author gives up all rights in this article. References: <boost1.38 Chinese document> Compiler: vs2008

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.