C++ Boost::array

  • 2020-04-02 02:56:29
  • OfStack

This article illustrates a container in c++ that supports static arrays: boost.array. Share with you for your reference. Specific analysis is as follows:

Many C++ programmers believe that boost.array is likely to appear in the next generation of standard libraries. A basic understanding of the usage of boost.array is essential.

1. Why do we need containers of fixed-size arrays

First, fixed-size arrays are still common, and while the STL provides vectors, they have a bit more overhead than static arrays as dynamically scalable arrays, which some people find intolerable. C++ also provides fixed-size array containers, and of course, the performance is comparable to that of regular arrays.

Boost. Array was created in response to this requirement.

Boost. Array implements most, but not all, of the reversable container requirements. The reason array is not an STL reversible container is that:

A constructor is not provided.
The element may have an uncertain initial value.
Swap () does not have constant complexity.
Size () is always constant based on the type of the second template parameter.
The container does not provide distributor support.

It does not implement the "sequence" requirement (see the C++ standard 23.1.1, [lib.sequence. Reqmts]), except for the following:

Provides front() and back().
The operator[] and at() are provided.

2. Header file and related member function declaration:
The Reference
The Header < Boost/array. HPP >
Class template array
Public construct/copy/destruct of array

The template < The typename U > Array & operator = (const array < U, N > & other);

Array iterator supports:

1. The iterator the begin ();
Const_iterator the begin () const;

Returns:

Iterator at the first element

Throws:

Do not throw an exception

2. The iterator end ();
Const_iterator end () const;

Returns:

Iterator after the last element

Throws:

Do not throw an exception

Array reverse iterator supports:

1. Reverse_iterator rbegin ();
Const_reverse_iterator rbegin () const;

Returns:

The reverse iterator of the first element in the reverse iteration

2. Reverse_iterator rend ();
Const_reverse_iterator rend () const;

Returns:

A reverse iterator located after the last element of the reverse iteration

Array capacity:

1. Size_type size ();

Returns:

N

2. The bool empty ();

Returns:

N==0

Throws:

Do not throw an exception

3. Size_type max_size ();

Returns:

N

Throws:

Do not throw an exception

Array element access:

1. The reference operator [] (size_type I);
Const_reference operator [] (size_type I) const;

Requires:

i < N

Returns:

The index for  i  The elements of the

Throws:

No exception is thrown.

2. The reference at (size_type I);
Const_reference ats (size_type I) const;

Returns:

The index for  i  The elements of the

Throws:

std::range_error if i >= N

3. The reference front ();
Const_reference front () const;

Requirements:

N > 0

Returns:

The first element

Throws:

Do not throw an exception

4. The reference back ();
Const_reference back () const;

Returns:

Iterator after the last element

Throws:

Do not throw an exception
0

5. Const T * data () const;

Returns:

Iterator after the last element

Throws:

Do not throw an exception
1

6. T * c_array ();

Returns:

Iterator after the last element

Throws:

Do not throw an exception
1

Array modifier:

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

Returns:

Iterator after the last element

Throws:

Do not throw an exception
3

2. The void the assign (const T & value);

Returns:

Iterator after the last element

Throws:

Do not throw an exception
4

Array special algorithm:

1. The template < Typename T, STD: : size_t N > Void swap (array < T, N > & x, array < T, N > & y);

Effect:

x.swap(y)

Throws:

No exception is thrown.
 
You can see that boost.array provides a common interface to the STL container. So it's easy to use. It's worth noting that boost doesn't provide custom constructors and copy constructors. But boost.array can be initialized by:

#include<boost/array.hpp>
#include<iostream>
using namespace std;
using namespace boost;
int main()
{
     array<int,6> a = {1,2,3,4,5,6};
     //Normal array consistent access form
     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;
    
     //Support the at () < br / >      cout << a.at(5) << endl;
   
     return 0;
 }

Hope that the article described in the C++ programming to help you.


Related articles: