[ array ]

The class template array<> is a replacement of C-array that supports some features required for sequence containers.

Constructors:

#include <tr1/array>
using std::tr1::array;

array<int, 3> arr;
array<int, 3> values = {1, 2, 3};

Nested type names:

template <class Ty, size_t N>
class array {
...
typedef Ty                                      value_type;
typedef value_type&                             reference;
typedef const value_type&                       const_reference;
typedef value_type*                             iterator;
typedef const value_type*                       const_iterator;
typedef std::size_t                             size_type;
typedef std::ptrdiff_t                          difference_type;
typedef std::reverse_iterator<iterator>         reverse_iterator;
typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
...

Member function size, max_size and empty:

size() and max_size() return the number of elements in the array:

size_type array<Ty, N>::size() const;            // return N

size_type array<Ty, N>::max_size() const;        // return N

bool array<Ty, N>::empty() const;                // return (N == 0)

Member function operator[], at:

operator[] returns a reference/const_reference to the element in the array at position i (the calling function is in charge to check the value of the index before calling these operators)

reference array<Ty,N>::operator[](size_type i);
const_reference array<Ty,N>::operator[] (size_type i) const;

the member functions at() return a reference/const_reference to the ith-element of the array or throw a std::out_of_range exception when the index is not valid:

reference array<Ty,N>::at(size_type i);
const_reference array<Ty,N>::at(size_type i) const;

Member functions assign and swap:

assign() replaces the elements of the array with a repetition of N elements:

void array<Ty,N>::assign(const Ty &value);

swap() the content of the arrays (O(n) complexity):

void array<Ty,N>::swap(array &rhs);

template <class Ty, size_t N>
void swap(array<Ty,N>& lhs, array<Ty,N>& rhs);

a.swap(b); 
swap(a,b);

Member functions front, back and data:

front() return a reference to the first element of the array. If the array is empty the behavior is implementation-defined.

reference array<Ty,N>::front();
const_reference array<Ty,N>::front() const;

back() return a reference to the last element of the array. If the array is empty the behavior is implementation-defined.

reference array<Ty,N>::back();
const_reference array<Ty,N>::back() const;

data() return a pointer to the first element of the array, or a NULL ptr if the array is empty.

Ty * array<Ty,N>::data();
const Ty * array<Ty,N>::data() const;

Member function and iterators:

return a random-access iterator that points to the first element of the array:

iterator array<Ty,N>::begin();
const_iterator array<Ty,N>::begin() const;

return a random-access iterator that points beyond the end of the array:

iterator array<Ty,N>::end();
const_iterator array<Ty,N>::end() const;

return a random-access iterator that points to the fist element of the reverse array (the last element of the sequence):

iterator array<Ty,N>::rbegin();
const_iterator array<Ty,N>::rbegin() const;

return a random-access iterator that points beyond the end of the reverse array (before the first element of the sequence):

iterator array<Ty,N>::rend();
const_iterator array<Ty,N>::rend() const;

Member function operator==, !=, <, <=, >, >=

template <class Ty, size_t N>
bool operator==( const array<Ty,N> &lhs, const array<Ty,N> &rhs);

template <class Ty, size_t N>
bool operator!=( const array<Ty,N> &lhs, const array<Ty,N> &rhs);

template <class Ty, size_t N>
bool operator<( const array<Ty,N> &lhs, const array<Ty,N> &rhs);

template <class Ty, size_t N>
bool operator<=( const array<Ty,N> &lhs, const array<Ty,N> &rhs);

template <class Ty, size_t N>
bool operator>( const array<Ty,N> &lhs, const array<Ty,N> &rhs);

template <class Ty, size_t N>
bool operator>=( const array<Ty,N> &lhs, const array<Ty,N> &rhs);

Template function get [ tuple-like ]:

template<int I, typename Ty, size_t N>
Ty& get(array<Ty, N> & arr);

template<int I, typename Ty, size_t N>
const Ty& get(const array<Ty, N>& arr);

get<0>(arr);
get<1>(arr);

Metafunction class tuple_element, tuple_size for array [ tuple-like ]

template <int I, typename Ty, size_t N>
class tuple_element< I, array<Ty,N> >;

template <typename Ty, size_t N>
class tuple_size< array<Ty,N> >;

typedef std::tr1::array<int, 3> myArray;

tuple_element<0, myArray>::type ...
tuple_size<myArray>::value ...

Comments