Back to General book
1. Std::array (c++11 support)
Template < class T, size_t N > class array;
Array class
An array container is a fixed-length sequence container that stores a certain number of elements in a strictly linear order.
The inside of an array container does not maintain any data other than the element itself (even without saving its own size, which is a template parameter that is determined at compile time). The array container has the same high efficiency as a normal array for storage space. The array class simply adds some members and global functions outside the array so that the arrays can be used as a standard container.
Unlike other standard containers, the array container has a fixed size and does not use allocator to manage allocations to the memory space of an element, the array container sets up a variety of members and encapsulates a fixed-length array. The array container cannot be dynamically extended or truncated (refer to the vector, which is a similar container that can be extended).
It is legal to define an array container of length 0, but you cannot dereference it (member Front,back, and data).
* Translator: The dereference is a reference to the reverse operation, as in the following example:
1 int a=0; 2 int* pa=&a; // The &a here is a reference operation 3 *pa=; // The *PA here is the dereference operation
Unlike other containers in the standard library, swapping * Two array containers involves exchanging each element of the container one by one, which is typically a very inefficient operation. On the other hand, such implementations make their iterators always remain associated with the original container.
* Translator: The swap operation is a method (function) provided by the array container that allows you to swap elements within two array containers, see swap.
Another unique feature of the array container is that it can be manipulated as a tuple object to manipulate the Get function in the:<array> header file, which can access the elements of the array container like a tuple, as well as using tuple_size and Tuple_ element to get the size and type of the array container.
2. Container Properties
-
Sequence
-
The elements in a sequence container are stored in strict linear order. When each element is accessed, it is accessed through the position in the sequence where the element is located.
-
Continuous storage space
-
The elements in a sequence container are stored in a contiguous memory space, so random access to arbitrary elements is the same time. Pointers to an element can access other elements through an offset address.
-
Fixed length
-
The container uses a constructor and a destructor to statically allocate the required space. The size of the container is determined at compile time. (runtime) There is no (extra) space or time overhead.
3. Template parameters
-
Parameter T
-
The type of the element that the container contains. is consistent with the class member Array::value_type.
-
Parameter n
-
The size of the array container, which represents the number of elements.
- In the reference or description of the array member function, these parameter names are defaulted as template parameters *.
-
* Translator: For example, here template < class T, size_t N > class array;
4. Member Types
- The following aliases are the member types of the array. They are widely used as types of parameter types and return values for member functions.
-
member Type |
definition |
Notes |
Value_type |
The first template parameter (T) |
|
Reference |
value_type& |
|
Const_reference |
Const value_type& |
|
Pointer |
value_type* |
|
Const_pointer |
Const value_type* |
|
Iterator |
A random access iterator to value_type |
Convertible to const_iterator |
Const_iterator |
A random access iterator to const Value_type |
|
Reverse_iterator |
Reverse_iterator<iterator> |
|
Const_reverse_iterator |
Reverse_iterator<const_iterator> |
|
Size_type |
size_t |
Unsigned integral type |
Difference_type |
ptrdiff_t |
Signed Integral type
|
5. member functions
Iterators
-
Begin
-
returns the iterator (public member function) that points to the front of the container
-
End
-
returns an iterator to the last end of the container (public member function)
-
Rbegin
-
returns a reverse iterator to the last end of the container (public member function)
-
Rend
-
returns a reverse iterator pointing to the front of the container (public member function)
-
Cbegin
-
returns a read-only iterator pointing to the front of the container (public member function)
-
Cend
-
returns a read-only iterator pointing to the end of the container (public member function)
-
Crbegin
-
returns a reverse read-only iterator to the end of the container (public member function)
-
Crend
-
returns a reverse read-only iterator pointing to the front of the container (public member function)
-
* Translator: Here the "front end" and "last End" refers to the first element in the container and the last element in the container, respectively. The read-only iterator itself can be added or subtracted, but the element pointed to by using a read-only iterator is read-only.
Capacity
-
Size
-
returns the number of elements (public member function)
-
Max_size
-
returns the maximum number of elements a container can store (public member function)
-
Empty
-
test whether the array container is empty (public member function)
-
* Translator: For array containers, size and max_size are always the same. For dynamically growing containers, for time performance reasons, when it is necessary to expand the container capacity, it is common practice to allocate more storage space than is actually required, so that there is no need to scale the container each time the element is inserted, in which case the size represents the number of elements, and the Max_ Size represents the capacity of the container, and they may be different.
Access to elements
-
Operator[]
-
Access Element (public member function)
-
At
-
Access Element (public member function)
-
Front
-
access the first element (public member function)
-
Back
-
access the last element (public member function)
-
Data
-
returns a pointer to the data * (public member function)
-
* Translator: The pointer returned by data is actually a pointer to the first element.
Modifier
-
Fill
-
populate the entire container with a value (public member function)
-
Swap
-
the contents of the interchange container (public member function)
6. Overloaded non-member functions
-
Get (Array)
-
Get element (tuple interface) (template function)
-
relational operators (Array)
Relational operators for array containers (template functions)
7. Special non-member class
-
Tuple_element<array>
Tuple elements of array type (special for class templates)
-
Tuple_size<array>
Tuple length attributes for array types (special for class templates)
Translation C + + STL container Reference Manual (chapter I <array>)