001 /****************************/
002 /* 1 次元配列 */
003 /* coded by Y.Suganuma */
004 /****************************/
005 #include <iostream>
006 #include <vector>
007 #include <array>
008
009 using namespace std;
010
011 int main()
012 {
013 // vector クラス
014 cout << "vector クラス\n";
015 vector<int> a1[2];
016 a1[0] = {1, 2, 3};
017 a1[1] = {4, 5, 6};
018 a1[1][0] = 10;
019 vector<int> a2 = a1[0]; // a1 の 1 行目をコピーした別の 1 次元配列
020 a2[1] = 100;
021 // 上の 2 行を下のように変更すれば,
022 // a1 の 1 行目と同じ配列になる.
023 // 出力部分もコメントのように変更する必要がある.
024 // 次に示す array クラスについても同様である.
025 // vector<int> *a2 = &a1[0]; // a1 の 1 行目と同じ 1 次元配列
026 // (*a2)[1] = 100;
027 vector<int> a3 = a1[1]; // a1 の 2 行目をコピーした別の 1 次元配列
028 cout << " 2 次元配列 a1: \n";
029 for (int i1 = 0; i1 < 2; i1++) {
030 cout << " ";
031 for (auto& x : a1[i1])
032 cout << " " << x;
033 cout << endl;
034 }
035 cout << " 1 次元配列 a2:";
036 for (auto& x : a2)
037 cout << " " << x;
038 // for (int i1 = 0; i1 < 3; i1++)
039 // cout << " " << (*a2)[i1];
040 cout << endl;
041 cout << " 1 次元配列 a3:";
042 for (auto& x : a3)
043 cout << " " << x;
044 cout << endl;
045 // array クラス
046 cout << "array クラス\n";
047 array<int, 3> b1[2];
048 b1[0] = {1, 2, 3};
049 b1[1] = {4, 5, 6};
050 b1[1][0] = 10;
051 array<int, 3> b2 = b1[0]; // b1 の 1 行目をコピーした別の 1 次元配列
052 b2[1] = 100;
053 array<int, 3> b3 = b1[1]; // b1 の 2 行目をコピーした別の 1 次元配列
054 cout << " 2 次元配列 b1: \n";
055 for (int i1 = 0; i1 < 2; i1++) {
056 cout << " ";
057 for (auto& x : b1[i1])
058 cout << " " << x;
059 cout << endl;
060 }
061 cout << " 1 次元配列 b2:";
062 for (auto& x : b2)
063 cout << " " << x;
064 cout << endl;
065 cout << " 1 次元配列 b3:";
066 for (auto& x : b3)
067 cout << " " << x;
068 cout << endl;
069 // 配列
070 cout << "配列\n";
071 int c1[][3] = {{1, 2, 3}, {4, 5, 6}};
072 c1[1][0] = 10;
073 int *c2 = c1[0]; // c1 の先頭アドレス(6 要素の 1 次元配列)
074 c2[1] = 100;
075 c2[3] = 1000; // 許される
076 int *c3 = c1[1]; // c1 の 2 行目先頭アドレス(3 要素の 1 次元配列)
077 cout << " 2 次元配列 c1: \n";
078 for (int i1 = 0; i1 < 2; i1++) {
079 cout << " ";
080 for (int i2 = 0; i2 < 3; i2++)
081 cout << " " << c1[i1][i2];
082 cout << endl;
083 }
084 cout << " 1 次元配列 c2:";
085 for (int i1 = 0; i1 < 6; i1++)
086 cout << " " << c2[i1];
087 cout << endl;
088 cout << " 1 次元配列 c3:";
089 for (int i1 = 0; i1 < 3; i1++)
090 cout << " " << c3[i1];
091 cout << endl;
092 // 配列(new 演算子)
093 cout << "配列(new 演算子)\n";
094 int **d1 = new int *[2];
095 d1[0] = new int [3] {1, 2, 3};
096 d1[1] = new int [3] {4, 5, 6};
097 d1[1][0] = 10;
098 int *d2 = d1[0]; // d1 の 1 行目の先頭アドレス(3 要素の 1 次元配列)
099 d2[1] = 100;
100 // d2[3] = 1000; // 添え字が大きすぎる(危険!)
101 int *d3 = d1[1]; // d1 の 2 行目先頭アドレス(3 要素の 1 次元配列)
102 cout << " 2 次元配列 d1: \n";
103 for (int i1 = 0; i1 < 2; i1++) {
104 cout << " ";
105 for (int i2 = 0; i2 < 3; i2++)
106 cout << " " << d1[i1][i2];
107 cout << endl;
108 }
109 cout << " 1 次元配列 d2:";
110 for (int i1 = 0; i1 < 3; i1++)
111 cout << " " << d2[i1];
112 cout << endl;
113 cout << " 1 次元配列 d3:";
114 for (int i1 = 0; i1 < 3; i1++)
115 cout << " " << d3[i1];
116 cout << endl;
117
118 return 0;
119 }