array クラス (C++ 標準ライブラリ)

長さ NTy 型の要素のシーケンスを制御するオブジェクトを記述します。 このシーケンスは、array<Ty, N> オブジェクト内に含まれる Ty の配列として格納されます。

構文

template <class Ty, std::size_t N>
class array;

パラメーター

Ty
要素の型。

N
要素の数。

メンバー

型の定義 説明
const_iterator 被制御シーケンスの定数反復子の型です。
const_pointer 要素への定数ポインターの型です。
const_reference 要素への定数参照の型です。
const_reverse_iterator 被制御シーケンスの定数反転反復子の型です。
difference_type 2 つの要素間の距離を表す、符号付きの型です。
iterator 被制御シーケンスの反復子の型です。
pointer 要素へのポインターの型です。
reference 要素への参照の型です。
reverse_iterator 被制御シーケンスの反転反復子の型です。
size_type 2 つの要素間の距離を表す、符号なしの型です。
value_type 要素の型。
メンバー関数 説明
array 配列オブジェクトを構築します。
assign (現在は使用されていません。.) を使用する fillすべての要素を置き換えます。
at 指定した位置にある要素にアクセスします。
back 最後の要素にアクセスします。
begin 被制御シーケンスの先頭を指定します。
cbegin 配列内の最初の要素を示すランダム アクセスの定数反復子を返します。
cend 配列の末尾の次の位置を指し示すランダム アクセス定数反復子を返します。
crbegin 反転された配列内の最初の要素への定数反復子を返します。
crend 反転された配列内の末尾の要素への定数反復子を返します。
data 最初の要素のアドレスを取得します。
empty 要素が存在するかどうかをテストします。
end 被制御シーケンスの末尾を指定します。
fill すべての要素を、指定された値に置き換えます。
front 最初の要素にアクセスします。
max_size 要素の数をカウントします。
rbegin 反転被制御シーケンスの先頭を指定します。
rend 反転被制御シーケンスの末尾を指定します。
size 要素の数をカウントします。
swap 2 つのコンテナーのコンテンツを交換します。
Operator 説明
array::operator= 被制御シーケンスを置き換えます。
array::operator[] 指定した位置にある要素にアクセスします。

解説

この型は、既定のコンストラクター array() と既定代入演算子 operator= を持ち、aggregate の要件を満たします。 そのため、array<Ty, N> 型のオブジェクトは、集計初期化子を使用して初期化できます。 たとえば、 にします。

array<int, 4> ai = { 1, 2, 3 };

このコードは、4 つの整数値を保持するオブジェクト ai を作成し、最初の 3 つの要素はそれぞれ値 1、2、3 に初期化し、4 番目の要素は 0 に初期化します。

必要条件

ヘッダー:<array>

名前空間std:

array::array

配列オブジェクトを構築します。

array();

array(const array& right);

パラメーター

right
挿入するオブジェクトまたは範囲。

解説

既定のコンストラクター array() は、被制御シーケンスを初期化されない状態 (または既定の初期化された状態) のままにします。 これを使用して、初期化されていない被制御シーケンスを指定します。

コピー コンストラクター array(const array& right) は、被制御シーケンスをシーケンス [right.begin(), right.end()) で初期化します。 これを使用して、配列オブジェクト right によって制御されるシーケンスのコピーである最初の被制御シーケンスを指定します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    Myarray c1(c0);

    // display contents " 0 1 2 3"
    for (const auto& it : c1)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0 1 2 3

array::assign

C++ 11 では廃止され、fill に置き換えられています。 すべての要素を置換します。

array::at

指定した位置にある要素にアクセスします。

reference at(size_type off);

constexpr const_reference at(size_type off) const;

パラメーター

off
アクセスする要素の位置。

解説

このメンバー関数は、off 位置にある被制御シーケンスの要素への参照を返します。 その位置が無効の場合、関数はクラス out_of_range のオブジェクトをスローします。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display odd elements " 1 3"
    std::cout << " " << c0.at(1);
    std::cout << " " << c0.at(3);
    std::cout << std::endl;

    return (0);
}

array::back

最後の要素にアクセスします。

reference back();

constexpr const_reference back() const;

解説

このメンバー関数は、被制御シーケンスの最後の要素への参照を返します。被制御シーケンスを空にすることはできません。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display last element " 3"
    std::cout << " " << c0.back();
    std::cout << std::endl;

    return (0);
}
0 1 2 3
3

array::begin

被制御シーケンスの先頭を指定します。

iterator begin() noexcept;
const_iterator begin() const noexcept;

解説

メンバー関数は、シーケンスの最初の要素 (または空のシーケンスの末尾の次の位置) を示すランダム アクセス反復子を返します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display first element " 0"
    Myarray::iterator it2 = c0.begin();
    std::cout << " " << *it2;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0

array::cbegin

範囲内の最初の要素を示す const 反復子を返します。

const_iterator cbegin() const noexcept;

戻り値

範囲の最初の要素、または空の範囲の末尾の次の位置 (空の範囲の場合、const) を指し示す cbegin() == cend() ランダム アクセス反復子。

解説

cbegin の戻り値で範囲内の要素を変更することはできません。

begin() メンバー関数の代わりにこのメンバー関数を使用して、戻り値が const_iterator になることを保証できます。 通常は、次の例に示すように auto 型推論キーワードと一緒に使用します。 例では、Containerconstbegin() をサポートする任意の種類の変更可能な (非 cbegin()) コンテナーであると見なします。

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();

// i2 is Container<T>::const_iterator

array::cend

範囲内の最後の要素の次の位置を指す const 反復子を返します。

const_iterator cend() const noexcept;

戻り値

範囲の末尾の次の位置を指し示す ランダム アクセス反復子。

解説

cend は、反復子が範囲の末尾を超えたかどうかをテストするために使用されます。

end() メンバー関数の代わりにこのメンバー関数を使用して、戻り値が const_iterator になることを保証できます。 通常は、次の例に示すように auto 型推論キーワードと一緒に使用します。 例では、Containerconstend() をサポートする任意の種類の変更可能な (非 cend()) コンテナーであると見なします。

auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();

// i2 is Container<T>::const_iterator

cend によって返された値は逆参照しないでください。

array::const_iterator

被制御シーケンスの定数反復子の型です。

typedef implementation-defined const_iterator;

解説

この型は、被制御シーケンスの定数ランダム アクセス反復子として使用できるオブジェクトを表します。

#include <array>
#include <iostream>

typedef std::array<int, 4> MyArray;

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

    // display contents " 0 1 2 3"
    std::cout << "it1:";
    for (MyArray::const_iterator it1 = c0.begin();
        it1 != c0.end();
        ++it1) {
        std::cout << " " << *it1;
    }
    std::cout << std::endl;

    // display first element " 0"
    MyArray::const_iterator it2 = c0.begin();
    std::cout << "it2:";
    std::cout << " " << *it2;
    std::cout << std::endl;

    return (0);
}
it1: 0 1 2 3
it2: 0

array::const_pointer

要素への定数ポインターの型です。

typedef const Ty *const_pointer;

解説

この型は、シーケンスの要素への定数ポインターとして使用できるオブジェクトを表します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display first element " 0"
    Myarray::const_pointer ptr = &*c0.begin();
    std::cout << " " << *ptr;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0

array::const_reference

要素への定数参照の型です。

typedef const Ty& const_reference;

解説

この型は、被制御シーケンスの要素への定数参照として使用できるオブジェクトを表します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display first element " 0"
    Myarray::const_reference ref = *c0.begin();
    std::cout << " " << ref;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0

array::const_reverse_iterator

被制御シーケンスの定数反転反復子の型です。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

解説

この型は、被制御シーケンスの定数反転反復子として使用できるオブジェクトを表します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display last element " 3"
    Myarray::const_reverse_iterator it2 = c0.rbegin();
    std::cout << " " << *it2;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
3

array::crbegin

反転された配列内の最初の要素への定数反復子を返します。

const_reverse_iterator crbegin() const;

戻り値

反転された配列の最初の要素を指すか、反転されていない配列の最後の要素だったものを指す、定数逆順ランダム アクセス反復子。

解説

戻り値を crbegin指定すると、配列オブジェクトを変更できません。

#include <array>
#include <iostream>

int main( )
{
   using namespace std;
   array<int, 2> v1 = {1, 2};
   array<int, 2>::iterator v1_Iter;
   array<int, 2>::const_reverse_iterator v1_rIter;

   v1_Iter = v1.begin( );
   cout << "The first element of array is "
        << *v1_Iter << "." << endl;

   v1_rIter = v1.crbegin( );
   cout << "The first element of the reversed array is "
        << *v1_rIter << "." << endl;
}
The first element of array is 1.
The first element of the reversed array is 2.

array::crend

逆順の配列内の最後の要素の次の位置を指す定数反復子を返します。

const_reverse_iterator crend() const noexcept;

戻り値

逆順の配列内の最後の要素の次の位置 (通常の順序の配列内の最初の要素の前の位置) を指す定数逆順ランダム アクセス反復子。

解説

crend は、配列で使用されるのと同様 array::cend に、反転された配列で使用されます。

戻り値 crend (適切にデクリメント) では、配列オブジェクトを変更できません。

crend を使用して、逆順反復子が配列の末尾に達したかどうかをテストできます。

crend によって返された値は逆参照しないでください。

#include <array>
#include <iostream>

int main( )
{
   using namespace std;
   array<int, 2> v1 = {1, 2};
   array<int, 2>::const_reverse_iterator v1_rIter;

   for ( v1_rIter = v1.rbegin( ) ; v1_rIter != v1.rend( ) ; v1_rIter++ )
      cout << *v1_rIter << endl;
}
2
1

array::data

最初の要素のアドレスを取得します。

Ty *data();

const Ty *data() const;

解説

メンバー関数は、被制御シーケンス内の最初の要素のアドレスを返します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display first element " 0"
    Myarray::pointer ptr = c0.data();
    std::cout << " " << *ptr;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0

array::difference_type

2 つの要素間の距離を表す、符号付きの型です。

typedef std::ptrdiff_t difference_type;

解説

符号付き整数型は、被制御シーケンス内にある 2 つの要素のアドレスの違いを表すことのできるオブジェクトを記述します。 これは型 std::ptrdiff_tのシノニムです。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display distance first-last " -4"
    Myarray::difference_type diff = c0.begin() - c0.end();
    std::cout << " " << diff;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
-4

array::empty

要素が存在しないかどうかをテストします。

constexpr bool empty() const;

解説

N == 0 の場合にのみ、メンバー関数は true を返します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display whether c0 is empty " false"
    std::cout << std::boolalpha << " " << c0.empty();
    std::cout << std::endl;

    std::array<int, 0> c1;

    // display whether c1 is empty " true"
    std::cout << std::boolalpha << " " << c1.empty();
    std::cout << std::endl;

    return (0);
}
0 1 2 3
false
true

array::end

被制御シーケンスの末尾を指定します。

reference end();

const_reference end() const;

解説

このメンバー関数は、シーケンスの最後を越えたところを示すランダム アクセス反復子を返します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display last element " 3"
    Myarray::iterator it2 = c0.end();
    std::cout << " " << *--it2;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
3

array::fill

配列を消去し、指定した要素を空の配列にコピーします。

void fill(const Type& val);

パラメーター

val
配列に挿入される要素の値。

解説

fill は、配列の各要素を、指定された値に置き換えます。

#include <array>
#include <iostream>

int main()
{
    using namespace std;
    array<int, 2> v1 = { 1, 2 };

    cout << "v1 = ";
    for (const auto& it : v1)
    {
        std::cout << " " << it;
    }
    cout << endl;

    v1.fill(3);
    cout << "v1 = ";
    for (const auto& it : v1)
    {
        std::cout << " " << it;
    }
    cout << endl;
}

array::front

最初の要素にアクセスします。

reference front();

constexpr const_reference front() const;

解説

このメンバー関数は、被制御シーケンスの最初の要素への参照を返します。被制御シーケンスを空にすることはできません。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display first element " 0"
    std::cout << " " << c0.front();
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0

array::iterator

被制御シーケンスの反復子の型です。

typedef implementation-defined iterator;

解説

この型は、被制御シーケンスのランダム アクセス反復子として使用できるオブジェクトを表します。

#include <array>
#include <iostream>

typedef std::array<int, 4> MyArray;

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

    // display contents " 0 1 2 3"
    std::cout << "it1:";
    for (MyArray::iterator it1 = c0.begin();
        it1 != c0.end();
        ++it1) {
        std::cout << " " << *it1;
    }
    std::cout << std::endl;

    // display first element " 0"
    MyArray::iterator it2 = c0.begin();
    std::cout << "it2:";
    std::cout << " " << *it2;
    std::cout << std::endl;

    return (0);
}
it1: 0 1 2 3

it2: 0

array::max_size

要素の数をカウントします。

constexpr size_type max_size() const;

解説

このメンバー関数は、N を返します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display (maximum) size " 4"
    std::cout << " " << c0.max_size();
    std::cout << std::endl;

    return (0);
}
0 1 2 3
4

array::operator[]

指定した位置にある要素にアクセスします。

reference operator[](size_type off);

constexpr const_reference operator[](size_type off) const;

パラメーター

off
アクセスする要素の位置。

解説

このメンバー関数は、off 位置にある被制御シーケンスの要素への参照を返します。 その位置が無効な場合、動作は定義されません。

の要素への参照を取得するために使用できるメンバー以外 getarray関数もあります。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display odd elements " 1 3"
    std::cout << " " << c0[1];
    std::cout << " " << c0[3];
    std::cout << std::endl;

    return (0);
}
0 1 2 3
1 3

array::operator=

被制御シーケンスを置き換えます。

array<Value> operator=(array<Value> right);

パラメーター

right
コピーするコンテナー。

解説

メンバー演算子は、right の各要素を被制御シーケンスの対応する要素に割り当ててから、*this を返します。 これを使用して、被制御シーケンスを right の被制御シーケンスのコピーと置き換えます。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    Myarray c1;
    c1 = c0;

    // display copied contents " 0 1 2 3"
        // display contents " 0 1 2 3"
    for (auto it : c1)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0 1 2 3

array::pointer

要素へのポインターの型です。

typedef Ty *pointer;

解説

この型は、シーケンスの要素へのポインターとして使用できるオブジェクトを表します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display first element " 0"
    Myarray::pointer ptr = &*c0.begin();
    std::cout << " " << *ptr;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0

array::rbegin

反転被制御シーケンスの先頭を指定します。

reverse_iterator rbegin()noexcept;
const_reverse_iterator rbegin() const noexcept;

解説

このメンバー関数は、被制御シーケンスの最後を越えたところを示す反転反復子を返します。 したがって、反転シーケンスの先頭を指定します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display last element " 3"
    Myarray::const_reverse_iterator it2 = c0.rbegin();
    std::cout << " " << *it2;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
3

array::reference

要素への参照の型です。

typedef Ty& reference;

解説

この型は、被制御シーケンスの要素への参照として機能するオブジェクトを表します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display first element " 0"
    Myarray::reference ref = *c0.begin();
    std::cout << " " << ref;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0

array::rend

反転被制御シーケンスの末尾を指定します。

reverse_iterator rend()noexcept;
const_reverse_iterator rend() const noexcept;

解説

メンバー関数は、シーケンスの最初の要素 (または空のシーケンスの末尾の次の位置) を示す反転反復子を返します。 したがって、反転シーケンスの末尾を指定します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display first element " 0"
    Myarray::const_reverse_iterator it2 = c0.rend();
    std::cout << " " << *--it2;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0

array::reverse_iterator

被制御シーケンスの反転反復子の型です。

typedef std::reverse_iterator<iterator> reverse_iterator;

解説

この型は、被制御シーケンスの反転反復子として使用できるオブジェクトを表します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display last element " 3"
    Myarray::reverse_iterator it2 = c0.rbegin();
    std::cout << " " << *it2;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
3

array::size

要素の数をカウントします。

constexpr size_type size() const;

解説

このメンバー関数は、N を返します。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display size " 4"
    std::cout << " " << c0.size();
    std::cout << std::endl;

    return (0);
}
0 1 2 3
4

array::size_type

2 つの要素間の距離を表す、符号なしの型です。

typedef std::size_t size_type;

解説

符号なし整数型は、被制御シーケンスの長さを表すことができるオブジェクトを表します。 これは型 std::size_tのシノニムです。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display distance last-first " 4"
    Myarray::size_type diff = c0.end() - c0.begin();
    std::cout << " " << diff;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
4

array::swap

この配列の内容を別の配列と交換します。

void swap(array& right);

パラメーター

right
コンテンツを交換する配列。

解説

このメンバー関数は、*thisright の間で被制御シーケンスを交換します。 それに比例して要素の割り当てとコンストラクターの呼び出しを N実行します。

2 つのarrayインスタンスをスワップするために使用できるメンバーswap以外の関数もあります。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    Myarray c1 = { 4, 5, 6, 7 };
    c0.swap(c1);

    // display swapped contents " 4 5 6 7"
    for (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    swap(c0, c1);

    // display swapped contents " 0 1 2 3"
    for (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    return (0);
}
0 1 2 3
4 5 6 7
0 1 2 3

array::value_type

要素の型。

typedef Ty value_type;

解説

この型は、テンプレート パラメーター Tyのシノニムです。

#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 (const auto& it : c0)
    {
        std::cout << " " << it;
    }
    std::cout << std::endl;

    // display contents " 0 1 2 3"
    for (const auto& it : c0)
    {
        Myarray::value_type val = it;
        std::cout << " " << val;
    }
    std::cout << std::endl;

    return (0);
}
0 1 2 3
0 1 2 3

関連項目

<array>