Misplaced Pages

Array (C++): Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editContent deleted Content addedVisualWikitext
Revision as of 09:23, 25 April 2010 editIvansorokin (talk | contribs)50 edits Overview of functions← 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}} 
(31 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>&lt;array&gt;</tt> in C++ standard library and in header <tt>&lt;boost/array.hpp&gt;</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.

=== Zero-sized arrays ===

Unlike standard arrays <tt>array</tt> class can have zero size. The effect of calling <tt>front()</tt> or <tt>back()</tt> for a zero-sized array is implementation defined. And <tt>begin() == end()</tt> shall be unique value. The return value of <tt>data()</tt> is unspecified.

== 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)
|}

Array class provides swap function and assignment operator. The only difference from other containers is that <tt>swap</tt> takes linear time.

{| 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)
|}

Although TR1 define function <tt>assign</tt> to fill array with specified value, C++0x has function <tt>fill</tt> intended for the same purpose. Boost implementation of array supports both functions.

{| class="wikitable" style="text-align: center"|
|-
! style="text-align: left" | expression
! return type
! description
! computational complexity
|-
| <tt>a.assign(u)</tt> || <tt>void</tt> || fill a with u (TR1 only) || ''O''(N)
|-
| <tt>a.fill(u)</tt> || <tt>void</tt> || fill a with u (C++0x only) || ''O''(N)
|}

Array class provides standard iterator interface.

{| class="wikitable" style="text-align: center"|
|-
! style="text-align: left" | expression
! return type
! description
! computational complexity
|-
| <tt>a.begin()</tt> || <tt>iterator</tt> || returns iterator to first element of array || ''O''(1)
|-
| <tt>a.end()</tt> || <tt>iterator</tt> || returns iterator to the one after the last element of array || ''O''(1)
|-
| <tt>a.rbegin()</tt> || <tt>reverse_iterator</tt> || returns reverse iterator to first element of array || ''O''(1)
|-
| <tt>a.rend()</tt> || <tt>reverse_iterator</tt> || returns reverse iterator to the one after the last element of array || ''O''(1)
|}

Array class provides standard query-capacity functions.

{| 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 provides standard set of element access functions.

{| class="wikitable" style="text-align: center"|
|-
! style="text-align: left" | expression
! return type
! description
! computational complexity
|-
| <tt>a</tt> || <tt>T &</tt> || returns reference to i-th element || ''O''(1)
|-
| <tt>a.at(i)</tt> || <tt>T &</tt> || returns reference to i-th element or throws out_of_range exception || ''O''(1)
|-
| <tt>a.front()</tt> || <tt>T &</tt> || returns reference to first element of array || ''O''(1)
|-
| <tt>a.back()</tt> || <tt>T &</tt> || returns reference to last element of array || ''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)
|}

Raw data access functions.

{| class="wikitable" style="text-align: center"|
|-
! style="text-align: left" | expression
! return type
! description
! computational complexity
|-
| <tt>a.data()</tt> || <tt>T *</tt> || returns pointer to first element of array || ''O''(1)
|}

== Links ==

*
*

]
]

Latest revision as of 11:58, 17 October 2021

Redirect to:

  • 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).