Revision as of 08:57, 25 April 2010 editIvansorokin (talk | contribs)50 edits +1← Previous edit | Latest revision as of 11:58, 17 October 2021 edit undoChristian75 (talk | contribs)Extended confirmed users, New page reviewers, Pending changes reviewers, Rollbackers114,301 edits {{R with history}} | ||
(39 intermediate revisions by 11 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT ] | |||
A array is wrapper class that provides ]-like interface to standard fixed-size C-arrays. It also overcomes several limitations of standard array. | |||
{{R to section}} | |||
== Creation History == | |||
{{R with 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 present 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 C++ standard library in ]. | |||
== Motivation == | |||
Standard C arrays has several principal limitation: | |||
* They aren't ]s. They can not be copied like any other object. | |||
* They do not obey standard <tt>operator &</tt> semantics. | |||
* They do not provide STL-like interface. | |||
The second item means that in the following code | |||
<source lang="cpp"> | |||
int a; | |||
int * b1 = a; | |||
int * b2 = &a; | |||
</source> | |||
<tt>b1</tt> and <tt>b2</tt> receive the same value. This behavior differs from one of any other standard type. | |||
== Design == | |||
Array template class is defined in header <tt><array></tt> in C++ standard library and in header <tt><boost/array.hpp></tt> in boost. It can resides in namespaces <tt>std::</tt> (in C++0x), <tt>std::tr1::</tt> (in C++03 with TR1) or <tt>boost::</tt>. | |||
The <tt>array</tt> class template is parametrized with a type of element and a 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 === | |||
<tt>array</tt> class is implemented as aggregate class. This allow array to be initialized with a brace-enclosing, 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 drawbacks: passing no initializer list means that the elements have an indetermined 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, array can be initialized as follows: | |||
<source lang="cpp"> | |||
array<int,4> a = { 1, 2, 3 }; | |||
</source> | |||
== Differences from standard array == | |||
* <tt>array</tt> class is value type. It satisfy <tt>CopyConstructable</tt> and <tt>Assignable</tt> requirements. | |||
* <tt>array</tt> class can not be implicitly casted to <tt>T *</tt> or <tt>T const *</tt>. However there is member function <tt>data()</tt> that returns pointer to first element. | |||
* <tt>array</tt> implementation is not required to do bound check. However implementation in boost do that for <tt>operator</tt>, but not for iterators. | |||
* Unlike standard arrays <tt>array</tt> class can have zero size. | |||
== Differences from standard containers == | |||
* <tt>array</tt> class do not provides constant-time swap. Instead it provides linear-time swap. | |||
* Because <tt>array</tt> class is aggregate it do not provides fill and range constructors. Its default constructor also does not initialize elements with zeros. | |||
* size() is always constant, based on the second template argument of the type. | |||
* The container provides no allocator support. | |||
== Overview of functions == | |||
Object of array class can be created using default constructor, copy constructor or initializer list syntax. | |||
{| class="wikitable" style="text-align: center"| | |||
|- | |||
! style="text-align: left" | expression | |||
! description | |||
! computational complexity | |||
|- | |||
| <tt>array a</tt> || create a array object, elements of that have undetermined values || ''O''(1) | |||
|- | |||
| <tt>array a1(a2)</tt> || create a copy of other array object || ''O''(N) | |||
|- | |||
| <tt>array a = {/*...*/}</tt> || create a array object initialized with specified values || ''O''(N) | |||
|} | |||
{| class="wikitable" style="text-align: center"| | |||
|- | |||
! style="text-align: left" | expression | |||
! return type | |||
! description | |||
! computational complexity | |||
|- | |||
| <tt>swap(a1, a2)</tt> || <tt>void</tt> || swap content of arrays || ''O''(N) | |||
|- | |||
| <tt>a1 = a2</tt> || <tt>array &</tt> || copy content of a2 to a1 || ''O''(N) | |||
|- | |||
| <tt>a.assign(u)</tt> || <tt>void</tt> || fill a with u || ''O''(N) | |||
|} | |||
{| class="wikitable" style="text-align: center"| | |||
|- | |||
! style="text-align: left" | expression | |||
! description | |||
! computational complexity | |||
|- | |||
| <tt>a.begin()</tt> || returns iterator to first element of array || ''O''(1) | |||
|- | |||
| <tt>a.end()</tt> || returns iterator to the one after the last element of array || ''O''(1) | |||
|- | |||
| <tt>a.rbegin()</tt> || returns reverse iterator to first element of array || ''O''(1) | |||
|- | |||
| <tt>a.rend()</tt> || returns reverse iterator to the one after the last element of array || ''O''(1) | |||
|} | |||
{| class="wikitable" style="text-align: center"| | |||
|- | |||
! style="text-align: left" | expression | |||
! return type | |||
! description | |||
! computational complexity | |||
|- | |||
| <tt>a.size()</tt> || <tt>size_t</tt> || returns size of array || ''O''(1) | |||
|- | |||
| <tt>a.max_size()</tt> || <tt>size_t</tt> || returns size of array || ''O''(1) | |||
|- | |||
| <tt>a.empty()</tt> || <tt>bool</tt> || <tt>a.size() == 0</tt> || ''O''(1) | |||
|} | |||
Array class has six comparison operators. | |||
{| class="wikitable" style="text-align: center"| | |||
|- | |||
! style="text-align: left" | expression | |||
! return type | |||
! description | |||
! computational complexity | |||
|- | |||
| <tt>a1 < a2</tt> || <tt>bool</tt> || compare arrays lexicographically || ''O''(N) | |||
|- | |||
| <tt>a1 == a2</tt> || <tt>bool</tt> || compare arrays lexicographically || ''O''(N) | |||
|- | |||
| <tt>a1 > a2</tt> || <tt>bool</tt> || compare arrays lexicographically || ''O''(N) | |||
|- | |||
| <tt>a1 <= a2</tt> || <tt>bool</tt> || compare arrays lexicographically || ''O''(N) | |||
|- | |||
| <tt>a1 != a2</tt> || <tt>bool</tt> || compare arrays lexicographically || ''O''(N) | |||
|- | |||
| <tt>a1 >= a2</tt> || <tt>bool</tt> || compare arrays lexicographically || ''O''(N) | |||
|} | |||
== Links == | |||
* | |||
* | |||
] | |||
] |
Latest revision as of 11:58, 17 October 2021
Redirect to:
- To a section: This is a redirect from a topic that does not have its own page to a section of a page on the subject. For redirects to embedded anchors on a page, use {{R to anchor}} instead.
- With history: This is a redirect from a page containing substantive page history. This page is kept as a redirect to preserve its former content and attributions. Please do not remove the tag that generates this text (unless the need to recreate content on this page has been demonstrated), nor delete this page.
- This template should not be used for redirects having some edit history but no meaningful content in their previous versions, nor for redirects created as a result of a page merge (use {{R from merge}} instead), nor for redirects from a title that forms a historic part of Misplaced Pages (use {{R with old history}} instead).