動的メモリの取得と可変配列(vector)

Listing. 1: 動的メモリの配列と可変配列
theMain.cpp
#include <iostream>
#include <vector>
 
using std::cout;
using std::cin;
using std::endl;
//using namespace std;
const int MAX_ARR_NUM = 10;
 
void getArray()
{
	int num;
	//動的配列のためのメモリアドレスの用意(ポインタ変数)
	int* pArr = nullptr; //配列メモリアドレスを用意
	//nullptrは初期化用なにも指していないメモリアドレスを表している
	cout << "数を入力:";
	cin >> num;
	//動的配列の取得 new
	pArr = new int[num]; //用意したメモリアドレスにint num個分の配列を展開
 
	for (int i = 0; i < num; i++)
		pArr[i] = i + 1;
 
	for (int i = 0; i < num; i++)
		cout << pArr[i] << " ";
 
	if(pArr!=nullptr)
		delete[] pArr;
}
 
int main()
{
	//静的配列
	int arr[MAX_ARR_NUM]={ 1,2,3,4,5,4,3,2,1,0 };
	std::vector<int> vInt;
	cout << "length:" << vInt.size() << endl;
	vInt.push_back(1);
	cout << "length:" << vInt.size() << endl;
	vInt.push_back(2);
	cout << "length:" << vInt.size() << endl;
	vInt.push_back(3);
	cout << "length:" << vInt.size() << endl;
	vInt.push_back(4);
	cout << "length:" << vInt.size() << endl;
 
	for (int i = 0; i < vInt.size(); i++)
		cout << vInt[i] << " ";
 
	for (auto e : vInt)
		cout << e << " ";
	return 0;
}
参考

動的オブジェクトの取得
vectorとかarrayとか