Skip to content

Latest commit

 

History

History
152 lines (110 loc) · 2.7 KB

array-functions.md

File metadata and controls

152 lines (110 loc) · 2.7 KB
descriptiontitlems.datef1_keywordsms.assetidhelpviewer_keywords
Learn more about: <array> functions
<array> functions
11/04/2016
array/std::array::get
array/std::get
array/std::swap
e0700a33-a833-4655-8735-16e71175efc8
std::array [C++], get
std::get [C++]
std::swap [C++]

<array> functions

The <array> header includes two non-member functions, get and swap, that operate on array objects.

get
swap

get

Returns a reference to the specified element of the array.

template <int Index, classT, size_t N> constexpr T& get(array<T, N>& arr) noexcept; template <int Index, classT, size_t N> constexprconst T& get(const array<T, N>& arr) noexcept; template <int Index, classT, size_t N> constexpr T&& get(array<T, N>&& arr) noexcept;

Parameters

Index
The element offset.

T
The type of an element.

N
The number of elements in the array.

arr
The array to select from.

Example

#include<array> #include<iostream>usingnamespacestd;typedef array<int, 4> MyArray; intmain() { MyArray c0 { 0, 1, 2, 3 }; // display contents " 0 1 2 3"for (constauto& e : c0) { cout << "" << e; } cout << endl; // display odd elements " 1 3" cout << "" << get<1>(c0); cout << "" << get<3>(c0) << endl; }
0 1 2 3 1 3 

swap

A non-member template specialization of std::swap that swaps two array objects.

template <classTy, std::size_t N> voidswap(array<Ty, N>& left, array<Ty, N>& right);

Parameters

Ty
The type of an element.

N
The size of the array.

left
The first array to swap.

right
The second array to swap.

Remarks

The template function executes left.swap(right).

Example

// std__array__swap.cpp// compile with: /EHsc #include<array> #include<iostream>typedef std::array<int, 4> Myarray; intmain() { Myarray c0 = { 0, 1, 2, 3 }; // display contents " 0 1 2 3"for (Myarray::const_iterator it = c0.begin(); it != c0.end(); ++it) std::cout << "" << *it; std::cout << std::endl; Myarray c1 = { 4, 5, 6, 7 }; c0.swap(c1); // display swapped contents " 4 5 6 7"for (Myarray::const_iterator it = c0.begin(); it != c0.end(); ++it) std::cout << "" << *it; std::cout << std::endl; swap(c0, c1); // display swapped contents " 0 1 2 3"for (Myarray::const_iterator it = c0.begin(); it != c0.end(); ++it) std::cout << "" << *it; std::cout << std::endl; return (0); }
0 1 2 3 4 5 6 7 0 1 2 3 

See also

<array>

close