template <class T, size_t N> struct array
#include <array>
using namespace std;
array <T, N> 変数名; // T : 変数の型,N : 要素の数
// 要素数が 3 で,全て要素が 0
array<int, 3> x;
// 最初の 3 個の要素を初期化子リストで初期化,残りの要素は 0
array<int, 5> y = {1, 2, 3};
// array y で初期化
array<int, 3> z = y;
== < <= != > >= []
#include <iostream>
#include <array>
using namespace std;
int main()
{
array<int, 5> a = {1, 2, 3};
array<int, 5> b = {a}; // コピーコンストラクタ
// 初期状態
cout << "初期状態 *a*"; // 下に示す b と同じ方法でも良い
for (auto x : a)
cout << " " << x;
cout << endl;
array<int, 5>::iterator it = b.begin();
cout << "初期状態 *b*";
for (int i1 = 0; i1 < (int)b.size(); i1++)
cout << " " << *it++;
// cout << " " << b[i1];
// cout << " " << b.at(i1);
cout << endl;
// b[4] に a[0] を代入
cout << "b[4] に a[0] を代入 *b*";
b.back() = a.front(); // b[b.size()-1] = a.front();
for (int i1 = 0; i1 < (int)b.size(); i1++)
cout << " " << b[i1];
cout << endl;
// a を 10 で満たす
a.fill(10);
cout << "a を 10 で満たす *a*";
for (auto x : a)
cout << " " << x;
cout << endl;
// swap
b.swap(a);
cout << "swap *a*";
for (auto x : a)
cout << " " << x;
cout << endl;
cout << "swap *b*";
for (auto x : b)
cout << " " << x;
cout << endl;
return 0;
}
初期状態 *a* 1 2 3 0 0 初期状態 *b* 1 2 3 0 0 b[4] に a[0] を代入 *b* 1 2 3 0 1 a を 10 で満たす *a* 10 10 10 10 10 swap *a* 1 2 3 0 1 swap *b* 10 10 10 10 10
| 菅沼ホーム | 本文目次 | 演習問題解答例 | 付録目次 | 索引 |