/*
*Definition:
*Author:
*Date:
*/
#include <iostream>
#include <array>
#include <vector>
using namespace std;
//Static array
int a[4][5] =
{ { 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 },
{ 16, 17, 18, 19, 20 } };
void showOne2Two(int *a)
{
int m_rows = 0, m_colums = 0;
m_rows = (sizeof(a) / sizeof(int)) / (sizeof(a[0]) / sizeof(int));
m_colums = sizeof(a) / sizeof(int);
for (int i = 0; i < m_rows; i++)
{
for (int j = 0; j < m_colums; j++)
{
cout << a[i*m_colums + j] << " , ";
}
cout << endl;
}
}
int main(){
//动态二维数组
//删除空间
int rows = 6, columns = 10;
int** a2 = new int*[rows];
for (int i = 0; i < rows; i++)
a2[i] = new int[columns];
//释放空间
for (int i = 0; i < rows; i++)
delete[]a2[i];
delete[]a2;
//随机动态二维数组(栈中)
vector<vector<int>>a3(rows, vector<int>(columns));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
a3[i][j] = a[i][j];//大数组,插入小数组。
}
}
//随机动态二维数(堆中)
//int *vector = new int[10][10];
//指针和new int[][]
int **p;
p = new int*[5];
for (int j = 0; j < 5; j++)
{
p[j] = new int[4];
}
//new int 54 的第四行四列是55
p[3][3] = 55;
for (int i = 0; i < 5; i++){
for (int j = 0; j < 4; j++){
cout << *(*(p + i) + j) << " , ";
}
cout << endl;
}
//在销毁的过程,先销毁指针数组每个元素指向的数组,然后再销毁这个指针数组。
for (int i = 0; i < 5; i++)
{
delete[] p[i];//先撤销指针元素所指向的数组
}
delete[]p;
//指针和array
int arrays[4][3] = {
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12},
};
int(*p2)[3] = arrays;
//输出三行三列
cout << "p2[2][2]:" << p2[2][2] << endl;
//关键字array 创建一维数组
std::array <int,5> a= {};
system("PAUSE");
return 0;
}
关于二维数组的原理:
int[3][2] 的意思是有一个数组有三个单元,一个单元存储两个int类型数据。