共用方式為


<array> 函式

陣列 <> 標頭包含兩個在陣列 物件上 運作的非成員函式 getswap

get
swap

get

傳回陣列中所指定元素的參考。

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

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

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

參數

Index
項目位移。

T
元素的類型。


陣列中的項目數。

arr
要從中選取的陣列。

範例

#include <array>
#include <iostream>

using namespace std;

typedef array<int, 4> MyArray;

int main()
{
    MyArray c0 { 0, 1, 2, 3 };

    // display contents " 0 1 2 3"
    for (const auto& 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

的非成員範本特製化 std::swap ,會交換兩 個數組 物件。

template <class Ty, std::size_t N>
void swap(array<Ty, N>& left, array<Ty, N>& right);

參數

Ty
元素的類型。


陣列的大小。

left
要交換的第一個陣列。

right
要交換的第二個陣列。

備註

樣板函式會執行 left.swap(right)

範例

// std__array__swap.cpp
// compile with: /EHsc
#include <array>
#include <iostream>

typedef std::array<int, 4> Myarray;
int main()
{
    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

另請參閱

<array>