#include<iostream>
#include<vector>
using namespace std;
int main()
{
//向量指针
vector<int>*a;
a = new vector<int>[5];
//相当于int *p = new int[5];即vector<int> *kkk=new vector<int>[5];
for (size_t i = 0; i < 5; i++)
{
for (size_t j = 0; j < 10; j++)
{
a[i].push_back(j);
}
}
for (size_t i = 0; i < 5; i++)
{
for (size_t j = 0; j < 10; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
//动态开辟二位数组
int** matrix;
matrix = new int* [5];
for (size_t i = 0; i < 5; i++)
{
//matrix[i] = new int* [10];
matrix[i] = new int[10];
for (size_t j = 0; j < 10; j++)
{
matrix[i][j] = j+10;
}
}
for (size_t i = 0; i < 5; i++)
{
for (size_t j = 0; j < 10; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
delete[] a;
for (size_t i = 0; i < 5; i++)
{
delete[]matrix[i];
}
delete []matrix;
system("pause");
return 0;
}
C++分别使用向量指针/动态开辟空间开辟二维数组
最新推荐文章于 2024-02-10 09:00:00 发布