Revision as of 13:10, 3 October 2011 edit1exec1 (talk | contribs)Pending changes reviewers, Rollbackers50,085 edits cleanup the overview of functions (we don't need so much details), move that section to details. Dependent types will be fixed later, comment them out for now← Previous edit | Revision as of 00:09, 6 December 2011 edit undo1exec1 (talk | contribs)Pending changes reviewers, Rollbackers50,085 edits per Talk:C++ Standard Library#reorgNext edit → | ||
Line 1: | Line 1: | ||
#REDIRECT ] | |||
{{lowercase}} | |||
{{C++ Standard library}} | |||
The '''array''' is a wrapper ] that provides an ]-like interface to standard fixed-size ] arrays. It also overcomes several limitations of standard arrays. | |||
== Creation history == | |||
In his book ''Generic Programming and the STL'', Matthew H. Austern introduces a wrapper class for ordinary arrays with static size, called <tt>block</tt>. It is safer and has no worse performance than ordinary arrays. In ''The C++ Programming Language, 3rd edition'', ] introduces a similar class, called <tt>c_array</tt>, which Nicolai Josuttis presents slightly modified in his book ''The C++ Standard Library - A Tutorial and Reference'', called <tt>carray</tt>. | |||
Under the name <tt>array</tt> this class is introduced in ] libraries by Nicolai Josuttis. Later this class was introduced in the ] in ]. | |||
== Motivation == | |||
Standard C arrays have several principal limitations: | |||
* They aren't ]s. They can not be copied like other objects. | |||
* They do not provide an STL-like interface. | |||
== Design == | |||
The <tt>array</tt> template class is defined in header <tt><array></tt> in the C++ standard library and in header <tt><boost/array.hpp></tt> in boost. It can reside in namespaces <tt>std::</tt> (in ]), <tt>std::tr1::</tt> (in C++03 with TR1) or <tt>boost::</tt>. | |||
The <tt>array</tt> class template is parametrized with the type of the elements and the number of elements. It can be instantiated with any type that fulfills the <tt>CopyConstructible</tt> and <tt>Assignable</tt> requirements. It also itself fulfills <tt>CopyConstructible</tt> and <tt>Assignable</tt> requirements. | |||
If <tt>array</tt> class template is instantiated with a type that fulfills <tt>EqualityComparable</tt> or <tt>LessThanComparable</tt> requirements, it fulfills <tt>EqualityComparable</tt> or <tt>LessThanComparable</tt> correspondingly. | |||
Class also provides standard iterators and element access functions. | |||
=== Implementation as aggregate === | |||
The <tt>array</tt> class is implemented as an aggregate class. This allows an array to be initialized with a brace-enclosed, comma-separated list of initializers for the elements of the container, written in increasing subscript order: | |||
<source lang="cpp"> | |||
array<int, 4> a = { { 1, 2, 3 } }; | |||
</source> | |||
Note that if there are fewer elements in the initializer list, then each remaining element gets default-initialized. (Thus, it has a defined value.) | |||
However, this approach has its own drawbacks: Passing no initializer list means that the elements have an indeterminate initial value, because the rule says that aggregates may have: | |||
* No user-declared constructors. | |||
* No private or protected non-static data members. | |||
* No base classes. | |||
* No virtual functions. | |||
Note that for standard conforming compilers it is possible to use fewer braces (according to 8.5.1 (11) of the Standard). That is, the <tt>array</tt> template can be initialized as follows: | |||
<source lang="cpp"> | |||
array<int, 4> a = { 1, 2, 3 }; | |||
</source> | |||
===Overview of functions=== | |||
*Element access | |||
**<code>array::</code> - Accesses specified element with bounds checking. | |||
**<code>array::</nowiki>]</code> - Accesses specified element | |||
**<code>array::</code> - Accesses the first element | |||
**<code>array::</code> - Accesses the last element | |||
**<code>array::</code> - Accesses the underlying array | |||
*Iterators | |||
**<code>array::</code> - Returns an iterator to the beginning of the array | |||
**<code>array::</code> - Returns an iterator to the end of the array | |||
**<code>array::</code> - Returns a reverse iterator to the reverse beginning of the array | |||
**<code>array::</code> - Returns a reverse iterator to the reverse end of the array | |||
* Capacity | |||
**<code>array::</code> - Checks whether the array is empty | |||
**<code>array::</code> - Returns the number of elements in the array. | |||
**<code>array::</code> - Returns the maximum possible number of elements in the array. | |||
*Modifiers | |||
**<code>array::</code> - Fills the array with the given value | |||
**<code>array::</code> - Swaps the contents with another array | |||
== Differences from standard array == | |||
* The <tt>array</tt> class is a value type. It satisfies <tt>CopyConstructable</tt> and <tt>Assignable</tt> requirements. | |||
* The <tt>array</tt> class can not be implicitly cast to <tt>T *</tt> or <tt>T const *</tt>. However there is member function <tt>data()</tt> that returns a pointer to the first element. | |||
* The <tt>array</tt> implementation is not required to do bound check. However the implementation in boost does that for <tt>operator</tt>, but not for iterators. | |||
=== Zero-sized arrays === | |||
Unlike standard arrays, the <tt>array</tt> class can have size zero. The effect of calling <tt>front()</tt> or <tt>back()</tt> for a zero-sized <tt>array</tt> is implementation-defined, but <tt>begin()</tt> and <tt>end()</tt> shall return the same unique value. The return value of <tt>data()</tt> is unspecified for zero-sized <tt>array</tt>s. | |||
== Differences from standard containers == | |||
* The <tt>array</tt> class does not provide constant-time swap. Instead it provides linear-time swap. | |||
* Because the <tt>array</tt> class is an aggregate it does not provide fill and range constructors. Its default constructor also does not initialize elements with zeros. | |||
* <tt>size()</tt> is always constant, based on the second template argument of the type. | |||
* The container provides no allocator support. | |||
<!-- | |||
== Overview of functions == | |||
The <tt>array</tt> class provides the standard set of dependent types. | |||
{| class="wikitable" style="text-align: center"| | |||
|- | |||
! style="text-align: left" | expression | |||
! return type | |||
|- | |||
| <tt>array<T, N>::reference</tt> || <tt>T &</tt> | |||
|- | |||
| <tt>array<T, N>::const_reference</tt> || <tt>T const &</tt> | |||
|- | |||
| <tt>array<T, N>::iterator</tt> || implementation-defined (<tt>T *</tt> in boost) | |||
|- | |||
| <tt>array<T, N>::const_iterator</tt> || implementation-defined (<tt>T const *</tt> in boost) | |||
|- | |||
| <tt>array<T, N>::size_type</tt> || <tt>size_t</tt> | |||
|- | |||
| <tt>array<T, N>::difference_type</tt> || <tt>ptrdiff_t</tt> | |||
|- | |||
| <tt>array<T, N>::value_type</tt> || <tt>T</tt> | |||
|- | |||
| <tt>array<T, N>::reverse_iterator</tt> || <tt>reverse_iterator<iterator></tt> | |||
|- | |||
| <tt>array<T, N>::const_reverse_iterator</tt> || <tt>reverse_iterator<const_iterator></tt> | |||
|} | |||
--> | |||
== External links == | |||
* | |||
* | |||
* | |||
] | |||
] |
Revision as of 00:09, 6 December 2011
Redirect to: