固定長配列と、動的配列と、可変長配列
#include
#include //可変長配列
using namespace std;
const int PLAYER_NUM{ 10 };
class samp
{
int n_;//<-プライベート
public:
samp() :n_(0) {}
samp(int _n):n_(_n){}
~samp(){}
void SetN(int _n) { n_ = _n; }
int GetN() { return(n_); }
};
int main()
{
//int arr[PLAYER_NUM]{ 2,4,6,8,10,12,14,16,18,20 };
//int num;
//cin >> num;
//int *player;
//player = new int[num];
//for (int i = 0; i < num; i++)
// player[i] = i + 1;
//cout << "固定長配列" << endl;
//for (int i = 0; i < PLAYER_NUM; i++)
// cout << arr[i] << " ";
//
//cout << endl;
//cout << "動的配列取得" << endl;
//for (int i = 0; i < num; i++)
// cout << player[i] << " ";
//delete[] player;
//samp Samp[PLAYER_NUM]{ 2,4,6,8,10,12,14,16,18,20 };
//for (int i = 0; i < PLAYER_NUM; i++)
// cout << Samp[i].GetN() << " ";
////オブジェクトの動的取得でオブジェクト数を決める!
//int num;
//cin >> num;
//int* initList = new int[num];
//for (int i = 0; i < num; i++) {
// cout << i << "番目の値:";
// cin >> initList[i];
//}
//samp* sampList = nullptr;
//sampList = new samp[num]; //sampList[0]~sampList[num-1]までが生成される
//for (int i = 0; i < num; i++)
// sampList[i].SetN(initList[i]);
//for (int i = 0; i < num; i++)
// cout << sampList[i].GetN() << " ";
vector sampList;
int n = 0;
while (true)
{
cout << "ただ今のsampListの長さ:" << sampList.size() << endl;
cin >> n;
if (n != -1) {
samp* p = new samp(n);
sampList.push_back(p);
}
else
break;
}
//sampListの中身のsmapのn_を全部表示
for (auto theI : sampList)
{
cout << theI->GetN() << " ";
}
for (auto theI : sampList)
delete theI;
cout << "ただ今のsampListの長さ:" << sampList.size() << endl;
sampList.clear();
cout << "ただ今のsampListの長さ:" << sampList.size() << endl;
return 0;
}
=== 今日やったソースコードの最後の姿 ===
配列を関数に引数で渡す
#include
#include //可変長配列
using namespace std;
const int PLAYER_NUM{ 10 };
class samp
{
int n_;//<-プライベート
public:
samp() :n_(0) {}
samp(int _n):n_(_n){}
~samp(){}
void SetN(int _n) { n_ = _n; }
int GetN() { return(n_); }
};
//配列は関数には引数として渡せない!
//(固定長)配列を関数に渡す
void PrintArray(int* p, int _num)//p:配列の先頭アドレス、_numは配列数
{
for (int i = 0; i < _num; i++)
cout << p[i] << " "; //cout << *(p + i) << endl;
cout << endl;
}
void PrintArray(samp *_samp, int _num)
{
for (int i = 0; i < _num; i++)
cout << _samp[i].GetN() << " "; //cout << *(_samp + i) << endl;
cout << endl;
}
//固定長配列と、動的配列で2つの関数を読んで、表示させてみる。
void PrintArray(vector& _samplist)
{
for (auto& theI : _samplist)//for-each C++では拡張for文 範囲for文
//for(int i = 0; i < _samplist.size(); i++)
{
cout << theI->GetN() << " ";
}
cout << endl;
}
int main()
{
int arr[PLAYER_NUM]{ 2,4,6,8,10,12,14,16,18,20 };
int num;
cin >> num;
int *player;
player = new int[num];
for (int i = 0; i < num; i++)
player[i] = i + 1;
cout << "固定長配列" << endl;
// for (int i = 0; i < PLAYER_NUM; i++)
// cout << arr[i] << " ";
PrintArray(arr, PLAYER_NUM);
//配列の先頭アドレス=配列名
//
// cout << endl;
cout << "動的配列取得" << endl;
// for (int i = 0; i < num; i++)
// cout << player[i] << " ";
PrintArray(player, num);
delete[] player;
samp Samp[PLAYER_NUM]{ 2,4,6,8,10,12,14,16,18,20 };
// for (int i = 0; i < PLAYER_NUM; i++)
// cout << Samp[i].GetN() << " ";
cout << "固定長オブジェクト配列" << endl; //クラスの、インスタンスの、固定長配列
PrintArray(Samp, PLAYER_NUM);
//オブジェクトの動的取得でオブジェクト数を決める!
//int num;
cin >> num;
int* initList = new int[num];
for (int i = 0; i < num; i++) {
cout << i << "番目の値:";
cin >> initList[i];
}
samp* sampList = nullptr;
sampList = new samp[num]; //sampList[0]~sampList[num-1]までが生成される
for (int i = 0; i < num; i++)
sampList[i].SetN(initList[i]);
PrintArray(sampList, num);
//// for (int i = 0; i < num; i++)
//// cout << sampList[i].GetN() << " ";
//
//
vector sampList2;
int n = 0;
while (true)
{
cout << "ただ今のsampListの長さ:" << sampList2.size() << endl;
cin >> n;
if (n != -1) {
samp* p = new samp(n);
sampList2.push_back(p);
}
else
break;
}
//sampListの中身のsmapのn_を全部表示
//for (auto theI : sampList2)
//{
// cout << theI->GetN() << " ";
//}
PrintArray(sampList2);
for (auto theI : sampList2)
delete theI;
cout << "ただ今のsampListの長さ:" << sampList2.size() << endl;
sampList2.clear();
cout << "ただ今のsampListの長さ:" << sampList2.size() << endl;
return 0;
}