function template
<array>

std::get (array)

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;
Get element (tuple interface)
Returns a reference to the Ith element of array arr.

This overload of tuple's homonym function get is provided so that array objects can be treated as tuples. For that purpose, header <array> also overloads tuple_size and tuple_element types with the appropriate members defined.

Template parameters

I
Position of an element in the array, with 0 as the position of the first element.
size_t is an unsigned integral type.
T
Type of elements contained in the array (generally obtained implicitly from arr).
N
Size of the array, in number of elements (generally obtained implicitly from arr).

Function parameters

arr
An array container.

Return value

A reference to the element at the specified position in the array.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// arrays as tuples
#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

Complexity

Constant.

Iterator validity

No changes.

Data races

Element I of arr is potentially accessed or modified by the caller. Concurrently accessing or modifying other elements of arr is safe.

Exception safety

No-throw guarantee: this function never throws exceptions.

See also