Arays are fixed-size sequential containers that hold a specific number of elements in a strict linear order.
Internally, an array does not save any data except the elements it contains (not even its size, it is a template parameter, fixed at compile time). Storage size is as efficient as a normal array declared in the language bracket grammar ([]). This class only adds a layer of member functions and global functions, so arrays can be used as standard containers.
Unlike other standard containers, arrays have a fixed size and do not manage the allocation of their elements through an allocator: they are aggregation types that encapsulate elements of fixed-size arrays. Therefore, they cannot expand or shrink dynamically.
Zero-sized arrays are valid, but they should not be dereferenced (front, back and data of members).
Unlike other containers in the standard library, swapping two array containers is a linear operation involving all elements within a single swap range, which is usually quite inefficient. On the other hand, this allows the iterator elements in both containers to remain associated with their original containers.
Another unique feature of array containers is that they can be treated as tuple objects: the array header overloads the get function to access array elements, just as it is a tuple, as well as specialized tuple_size and tuple_element types.
template < class T, size_t N > class array;
Method | Meaning |
---|---|
begin | Returns an iterator pointing to the first element in the array container |
end | Returns an iterator that points to the theoretical element after the last element in the array container |
rbegin | Returns the reverse iterator pointing to the last element in the array container |
rend | Returns a reverse iterator pointing to the theoretical element before the first element in the array |
cbegin | Returns a const_iterator pointing to the first element in the array container |
cend | Returns a const_iterator pointing to the theoretical element after the last element in the array container |
crbegin | Returns the const_reverse_iterator pointing to the last element in the array container |
crend | Returns the const_reverse_iterator pointing to the theoretical element before the first element in the array |
size | Returns the number of elements in the array container |
max_size | Returns the maximum number of elements that an array container can hold |
empty | Returns a Boolean value indicating whether the array container is empty |
operator[] | Returns a reference to the element at the n (parameter) position in the container |
at | Returns a reference to the element at the n (parameter) position in the container |
front | Returns a reference to the first element in the container |
back | Returns a reference to the last element in the container |
data | Returns a pointer to the first element in the container |
fill | Fill all elements of the array with val (parameter) |
swap | Exchange the contents of an array through the contents of x (parameters) |
get(array) | Like std:: get < 0 > (myarray); passes in an array container, returning a reference to the specified location element |
relational operators (array) | ArayA > ArayB; compares the size of each element in an array |
1. array::begin
Returns an iterator that points to the first element in the array container.
iterator begin() noexcept; const_iterator begin() const noexcept;
Example
#include <iostream> #include <array> int main() { std::array<int, 5> myarray = {2, 16, 77,34, 50}; std::cout << "myarray contains:"; for(auto it = myarray.begin(); it != myarray.end(); ++i) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Output
myarray contains: 2 16 77 34 50
2. array::end
Returns an iterator that points to the theoretical element after the last element in the array container.
iterator end() noexcept; const_iterator end() const noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,5> myarray = { 5, 19, 77, 34, 99 }; std::cout << "myarray contains:"; for ( auto it = myarray.begin(); it != myarray.end(); ++it ) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Output
myarray contains: 5 19 77 34 99
3. array::rbegin
Returns the reverse iterator pointing to the last element in the array container.
reverse_iterator rbegin()noexcept; const_reverse_iterator rbegin()const noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,4> myarray = {4, 26, 80, 14} ; for(auto rit = myarray.rbegin(); rit < myarray.rend(); ++rit) std::cout << ' ' << *rit; std::cout << '\n'; return 0; }
Output
myarray contains: 14 80 26 4
4. array::rend
Returns a reverse iterator that points to the theoretical element before the first element in the array (which is considered its reverse end).
reverse_iterator rend() noexcept; const_reverse_iterator rend() const noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,4> myarray = {4, 26, 80, 14}; std::cout << "myarray contains"; for(auto rit = myarray.rbegin(); rit < myarray.rend(); ++rit) std::cout << ' ' << *rit; std::cout << '\n'; return 0; }
Output
myarray contains: 14 80 26 4
5. array::cbegin
Returns a const_iterator pointing to the first element in the array container; this iterator can be added and reduced, but cannot be used to modify what it points to.
const_iterator cbegin()const noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,5> myarray = {2, 16, 77, 34, 50}; std::cout << "myarray contains:"; for ( auto it = myarray.cbegin(); it != myarray.cend(); ++it ) std::cout << ' ' << *it; // cannot modify *it std::cout << '\n'; return 0; }
Output
myarray contains: 2 16 77 34 50
6. array::cend
Returns a const_iterator pointing to the theoretical element after the last element in the array container. This iterator can be added and reduced, but it cannot be used to modify what it points to.
const_iterator cend() const noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,5> myarray = { 15, 720, 801, 1002, 3502 }; std::cout << "myarray contains:"; for ( auto it = myarray.cbegin(); it != myarray.cend(); ++it ) std::cout << ' ' << *it; // cannot modify *it std::cout << '\n'; return 0; }
Output
myarray contains: 2 16 77 34 50
7. array::crbegin
Returns the const_reverse_iterator pointing to the last element in the array container
const_reverse_iterator crbegin()const noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,6> myarray = {10, 20, 30, 40, 50, 60} ; std::cout << "myarray backwards:"; for ( auto rit=myarray.crbegin() ; rit < myarray.crend(); ++rit ) std::cout << ' ' << *rit; // cannot modify *rit std::cout << '\n'; return 0; }
Output
myarray backwards: 60 50 40 30 20 10
8. array::crend
Returns a const_reverse_iterator pointing to the theoretical element before the first element in the array, which is considered to end in the opposite direction.
const_reverse_iterator crend() const noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,6> myarray = {10, 20, 30, 40, 50, 60} ; std::cout << "myarray backwards:"; for ( auto rit=myarray.crbegin() ; rit < myarray.crend(); ++rit ) std::cout << ' ' << *rit; // cannot modify *rit std::cout << '\n'; return 0; }
Output
myarray backwards: 60 50 40 30 20 10
9. array::size
Returns the number of elements in the array container.
constexpr size_type size()noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,5> myints; std::cout << "size of myints:" << myints.size() << std::endl; std::cout << "sizeof(myints):" << sizeof(myints) << std::endl; return 0; }
Possible Output
size of myints: 5 sizeof(myints): 20
10. array::max_size
Returns the maximum number of elements that an array container can hold. Like its size, the max_size of an array object is always equal to the second template parameter used to instantiate the array template class.
constexpr size_type max_size() noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,10> myints; std::cout << "size of myints: " << myints.size() << '\n'; std::cout << "max_size of myints: " << myints.max_size() << '\n'; return 0; }
Output
size of myints: 10 max_size of myints: 10
11. array::empty
Returns a Boolean value indicating whether the array container is empty, that is, whether its size() is 0.
constexpr bool empty() noexcept;
Example
#include <iostream> #include <array> int main () { std::array<int,0> first; std::array<int,5> second; std::cout << "first " << (first.empty() ? "is empty" : "is not empty") << '\n'; std::cout << "second " << (second.empty() ? "is empty" : "is not empty") << '\n'; return 0; }
Output:
first is empty second is not empt
12. array::operator[]
Returns a reference to the element in the nth position in the array. Similar to array::at, array::at checks array boundaries and throws an out_of_range exception to determine whether n is out of range, while array::operator [] does not check boundaries.
reference operator[] (size_type n); const_reference operator[] (size_type n) const;
Example
#include <iostream> #include <array> int main () { std::array<int,10> myarray; unsigned int i; // assign some values: for(i=0; i<10; i++) myarray[i] = i; // print content std::cout << "myarray contains:"; for(i=0; i<10; i++) std::cout << ' ' << myarray[i]; std::cout << '\n'; return 0; }
Output
myarray contains: 0 1 2 3 4 5 6 7 8 9
13. array::at
Returns a reference to the element in the nth position in the array. Similar to array::operator [], but array::at checks array boundaries and throws an out_of_range exception to determine whether n is out of range, while array::operator [] does not check boundaries.
reference at ( size_type n ); const_reference at ( size_type n ) const;
Example
#include <iostream> #include <array> int main() { std::array<int, 10> myarray; unsigned int i; // assign some values: for (i = 0; i<10; i++) myarray[i] = i; // print content std::cout << "myarray contains:"; for (i = 0; i<10; i++) std::cout << ' ' << myarray.at(i); std::cout << '\n'; return 0; }
Output
myarray contains: 0 1 2 3 4 5 6 7 8 9
14. array::front
Returns a reference to the first element in the array container. array::begin returns an iterator, and array::front returns a direct reference.
Calling this function on an empty container results in undefined behavior.
reference front(); const_reference front() const;
Example
#include <iostream> #include <array> int main () { std::array<int,3> myarray = {2, 16, 77}; std::cout << "front is: " << myarray.front() << std::endl; // 2 std::cout << "back is: " << myarray.back() << std::endl; // 77 myarray.front() = 100; std::cout << "myarray now contains:"; for ( int& x : myarray ) std::cout << ' ' << x; std::cout << '\n'; return 0; }
Output
front is: 2 back is: 77 myarray now contains: 100 16 77
15. array::back
Returns a reference to the last element in the array container. array::end returns an iterator, and array::back returns a direct reference.
Calling this function on an empty container results in undefined behavior.
reference back(); const_reference back() const;
Example
#include <iostream> #include <array> int main () { std::array<int,3> myarray = {5, 19, 77}; std::cout << "front is: " << myarray.front() << std::endl; // 5 std::cout << "back is: " << myarray.back() << std::endl; // 77 myarray.back() = 50; std::cout << "myarray now contains:"; for ( int& x : myarray ) std::cout << ' ' << x; std::cout << '\n'; return 0; }
Output
front is: 5 back is: 77 myarray now contains: 5 19 50
16. array::data
Returns a pointer to the first element in an array object.
Since elements in an array are stored in consecutive storage locations, the retrieved pointer can be offset to access any element in the array.
value_type* data() noexcept; const value_type* data() const noexcept;
Example
#include <iostream> #include <cstring> #include <array> int main () { const char* cstr = "Test string"; std::array<char,12> charray; std::memcpy (charray.data(),cstr,12); std::cout << charray.data() << '\n'; return 0; }
Output
Test string
17. array::fill
Fill in all elements of the array with val, and set Val to the value of all elements in the array object.
void fill (const value_type& val);
Example
#include <iostream> #include <array> int main () { std::array<int,6> myarray; myarray.fill(5); std::cout << "myarray contains:"; for ( int& x : myarray) { std::cout << ' ' << x; } std::cout << '\n'; return 0; }
Output
myarray contains: 5 5 5 5 5 5
18. array::swap
Exchange the contents of an array through the contents of x, which is another array object of the same type (including the same size).
Unlike the exchange member functions of other containers, this member function runs in linear time by performing separate exchange operations of the same size between elements.
void swap (array& x) noexcept(noexcept(swap(declval<value_type&>(),declval<value_type&>())));
Example
#include <iostream> #include <array> int main () { std::array<int,5> first = {10, 20, 30, 40, 50}; std::array<int,5> second = {11, 22, 33, 44, 55}; first.swap (second); std::cout << "first:"; for (int& x : first) std::cout << ' ' << x; std::cout << '\n'; std::cout << "second:"; for (int& x : second) std::cout << ' ' << x; std::cout << '\n'; return 0; }
Output
first: 11 22 33 44 55 second: 10 20 30 40 50
19. get(array)
For example: std:: get < 0 > (myarray); pass in an array container, return a reference to the specified location element.
template <size_t I,class T,size_t N> T&get(array <T,N>&arr)noexcept; template <size_t I,class T,size_t N> T && get(array <T,N> && arr)noexcept; template <size_t I,class T,size_t N> const T&get(const array <T,N>&arr)noexcept;
Example
#include <iostream> #include <array> #include <tuple> int main () { std::array<int,3> myarray = {10, 20, 30}; std::tuple<int,int,int> mytuple (10, 20, 30); std::tuple_element<0,decltype(myarray)>::type myelement; // int myelement myelement = std::get<2>(myarray); std::get<2>(myarray) = std::get<0>(myarray); std::get<0>(myarray) = myelement; std::cout << "first element in myarray: " << std::get<0>(myarray) << "\n"; std::cout << "first element in mytuple: " << std::get<0>(mytuple) << "\n"; return 0; }
Output
first element in myarray: 30 first element in mytuple: 10
20. relational operators (array)
For example: arrayA!= arrayB, arrayA > arrayB; compare the size of each element of the array.
(1) template <class T,size_T N> bool operator ==(const array <T,N>&lhs,const array <T,N>&rhs); (2) template <class T,size_T N> bool operator!=(const array <T,N>&lhs,const array <T,N>&rhs); (3) template <class T,size_T N> bool operator <(const array <T,N>&lhs,const array <T,N>&rhs); (4) template <class T,size_T N> bool operator <=(const array <T,N>&lhs,const array <T,N>&rhs); (5) template <class T,size_T N> bool operator>(const array <T,N>&lhs,const array <T,N>&rhs); (6) template <class T,size_T N> bool operator> =(const array <T,N>&lhs,const array <T,N>&rhs);
Example
#include <iostream> #include <array> int main () { std::array<int,5> a = {10, 20, 30, 40, 50}; std::array<int,5> b = {10, 20, 30, 40, 50}; std::array<int,5> c = {50, 40, 30, 20, 10}; if (a==b) std::cout << "a and b are equal\n"; if (b!=c) std::cout << "b and c are not equal\n"; if (b<c) std::cout << "b is less than c\n"; if (c>b) std::cout << "c is greater than b\n"; if (a<=b) std::cout << "a is less than or equal to b\n"; if (a>=b) std::cout << "a is greater than or equal to b\n"; return 0; }
Output
a and b are equal b and c are not equal b is less than c c is greater than b a is less than or equal to b a is greater than or equal to b