Misplaced Pages

array (C++)

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.

This is an old revision of this page, as edited by 1exec1 (talk | contribs) at 13:10, 3 October 2011 (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). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 13:10, 3 October 2011 by 1exec1 (talk | contribs) (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)(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
C++ Standard Library
Containers
C standard library

The array is a wrapper class that provides an STL-like interface to standard fixed-size C 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 block. It is safer and has no worse performance than ordinary arrays. In The C++ Programming Language, 3rd edition, Bjarne Stroustrup introduces a similar class, called c_array, which Nicolai Josuttis presents slightly modified in his book The C++ Standard Library - A Tutorial and Reference, called carray.

Under the name array this class is introduced in boost libraries by Nicolai Josuttis. Later this class was introduced in the C++ Standard Library in TR1.

Motivation

Standard C arrays have several principal limitations:

  • They aren't value types. They can not be copied like other objects.
  • They do not provide an STL-like interface.

Design

The array template class is defined in header <array> in the C++ standard library and in header <boost/array.hpp> in boost. It can reside in namespaces std:: (in C++0x), std::tr1:: (in C++03 with TR1) or boost::.

The array 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 CopyConstructible and Assignable requirements. It also itself fulfills CopyConstructible and Assignable requirements.

If array class template is instantiated with a type that fulfills EqualityComparable or LessThanComparable requirements, it fulfills EqualityComparable or LessThanComparable correspondingly.

Class also provides standard iterators and element access functions.

Implementation as aggregate

The array 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:

array<int, 4> a = { { 1, 2, 3 } };

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 array template can be initialized as follows:

array<int, 4> a = { 1, 2, 3 };

Overview of functions

  • Element access
    • array::at - Accesses specified element with bounds checking.
    • array::operator - Accesses specified element
    • array::front - Accesses the first element
    • array::back - Accesses the last element
    • array::data - Accesses the underlying array
  • Iterators
    • array::begin - Returns an iterator to the beginning of the array
    • array::end - Returns an iterator to the end of the array
    • array::rbegin - Returns a reverse iterator to the reverse beginning of the array
    • array::rend - Returns a reverse iterator to the reverse end of the array
  • Capacity
    • array::empty - Checks whether the array is empty
    • array::size - Returns the number of elements in the array.
    • array::max_size - Returns the maximum possible number of elements in the array.
  • Modifiers
    • array::fill - Fills the array with the given value
    • array::swap - Swaps the contents with another array

Differences from standard array

  • The array class is a value type. It satisfies CopyConstructable and Assignable requirements.
  • The array class can not be implicitly cast to T * or T const *. However there is member function data() that returns a pointer to the first element.
  • The array implementation is not required to do bound check. However the implementation in boost does that for operator, but not for iterators.

Zero-sized arrays

Unlike standard arrays, the array class can have size zero. The effect of calling front() or back() for a zero-sized array is implementation-defined, but begin() and end() shall return the same unique value. The return value of data() is unspecified for zero-sized arrays.

Differences from standard containers

  • The array class does not provide constant-time swap. Instead it provides linear-time swap.
  • Because the array class is an aggregate it does not provide 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.

External links

Category: