The examples in this article describe the containers that support static arrays in C + +: Boost.array. Share to everyone for your reference. The specific analysis is as follows:
Many C + + programmers believe that Boost.array is likely to appear in next-generation standard libraries. It is necessary to have a basic understanding of the usage of Boost.array.
1. Why do we need containers of fixed-size arrays
First, fixed-size arrays are still common, although the STL provides vectors, but vectors, as dynamically scalable arrays, have a bit more overhead than static arrays, which some people can't stand. C + + also need to provide a fixed size capacity of the array container, of course, performance can be compared to the normal array.
Boost.array should be born with this demand.
Boost.array implements most but not all of the "reversible containers (reversable container)" Requirements. Array is not an STL reversible container because:
① does not provide a constructor.
The ② element may have an indeterminate initial value.
③swap () is not of constant complexity.
④size () is always constant based on the type of the second template parameter.
The ⑤ container does not provide allocator support.
It does not implement the requirements for "sequence" (see also the C + + standard 23.1.1, [lib. sequence.reqmts]), except for the following:
① provides front () and back ().
The ② provides operator[] and at ().
2. header file and related member function declaration:
Reference
Header <boost/array.hpp>
Class template Array (class template array)
Array-owned construction/copy/destructor
Template<typename u> array& operator= (const array<u, n>& other);
Array iterator support:
1.iterator begin ();
Const_iterator begin () const;
Return: |
The iterator at the first element |
Thrown: |
Do not throw an exception |
2.iterator end ();
Const_iterator end () const;
Return: |
An iterator that is positioned after the last element |
Thrown: |
Do not throw an exception |
Array Reverse iterator support:
1.reverse_iterator Rbegin ();
Const_reverse_iterator rbegin () const;
Return: |
The reverse iterator of the first element in the reverse iteration |
2.reverse_iterator rend ();
Const_reverse_iterator rend () const;
Return: |
The reverse iterator that is positioned after the last element of the reverse iteration |
Array Capacity:
1.size_type size ();
2.bool empty ();
Return: |
N==0 |
Thrown: |
Do not throw an exception |
3.size_type max_size ();
Return: |
N |
Thrown: |
Do not throw an exception |
Array element access:
1.reference operator[] (size_type i);
Const_reference operator[] (size_type i) const;
Requires: |
i < N |
Return: |
i The element that is indexed |
Thrown: |
Do not throw an exception. |
2.reference at (size_type i);
Const_reference at (size_type i) const;
Return: |
i The element that is indexed |
Thrown: |
std::range_error if i >= N |
3.reference Front ();
Const_reference Front () const;
Requirements: |
N > 0 |
Return: |
First element |
Thrown: |
Do not throw an exception |
4.reference back ();
Const_reference back () const;
Requirements: |
N > 0 |
Return: |
Last element |
Thrown: |
Do not throw an exception |
5.const t* data () const;
Return: |
elems |
Thrown: |
Do not throw an exception |
6.t* C_array ();
Return: |
elems |
Thrown: |
Do not throw an exception |
Array modifier:
1.void swap (array<t, n>& other);
Effect: |
std::swap_ranges(begin(), end(), other.begin()) |
Degree of complexity: |
Based on N the linear growth |
2.void Assign (const t& value);
Effect: |
std::fill_n(begin(), N, value) |
Array Special algorithm:
1.template<typename T, std::size_t n> void Swap (array<t, n>& x, array<t, n>& y);
Effect: |
x.swap(y) |
Thrown: |
Do not throw an exception. |
Visible Boost.array provides a common interface with STL containers. So it's easy to use. It is worth mentioning that boost does not provide custom constructors and copy constructors. But Boost.array can initialize this way:
Copy Code code as follows:
#include <boost/array.hpp>
#include <iostream>
using namespace Std;
using namespace boost;
int main ()
{
Array<int,6> a = {1,2,3,4,5,6};
Consistent access form for normal arrays
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 at ()
cout << a.at (5) << Endl;
return 0;
}
I hope this article will help you with the C + + program design.