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:
- Provided
front
() Andback
().
- Provided
operator[]
Andat
().
2. header file and related member function declaration: Reference
Header <boost/array. HPP>
array
template<typename U> array& operator=(const array<U, N>& other);
Class template array (class template array)Public constructor/copy/destructor
array
Iterator support
iterator begin();const_iterator begin() const;
Return Value: |
Iterator located in the first element |
Throw: |
Do not throw an exception |
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
reverse_iterator rbegin();const_reverse_iterator rbegin() const;
Return Value: |
The reverse iterator located in the first element of the reverse Iteration |
reverse_iterator rend();const_reverse_iterator rend() const;
Return Value: |
The reverse iterator located after the last element of the reverse Iteration |
array
Capacity
size_type size();
bool empty();
Return Value: |
N==0 |
Throw: |
Do not throw an exception |
size_type max_size();
Return Value: |
N |
Throw: |
Do not throw an exception |
array
Element access
reference operator[](size_type i);const_reference operator[](size_type i) const;
Requires: |
i < N |
Return Value: |
Index isi Element |
Throw: |
No exception is thrown. |
reference at(size_type i);const_reference at(size_type i) const;
Return Value: |
Index isi Element |
Throw: |
std::range_error Ifi >= N |
reference front();const_reference front() const;
Requirements: |
N > 0 |
Return Value: |
First element |
Throw: |
Do not throw an exception |
reference back();const_reference back() const;
Requirements: |
N > 0 |
Return Value: |
Last Element |
Throw: |
Do not throw an exception |
const T* data() const;
Return Value: |
elems |
Throw: |
Do not throw an exception |
T* c_array();
Return Value: |
elems |
Throw: |
Do not throw an exception |
array
Modifier
void swap(array<T, N>& other);
Effect: |
std::swap_ranges(begin(), end(), other.begin()) |
Complexity: |
Based onN Linear growth |
void assign(const T& value);
Effect: |
std::fill_n(begin(), N, value) |
array
Special Algorithms
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