当前位置:网站首页>50.【动态二维数组的运用】
50.【动态二维数组的运用】
2022-08-01 09:25:00 【李在奋斗……】

==============
【列数只能手动输入,行数可以动态】
#include <iostream>
using namespace std;
int main()
{
int m, n;
cout << "请输入行数为:" << endl;
cin >> m;
cout << "请输入列数为:" << endl;
cin >> n;
int(*p)[n]; // 列数不能动态输入
p = new int[m][n];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "请输入p[" << i << "][" << j << "]的值" << endl;
cin >> p[i][j];
}
}
cout << "输出为:" << endl;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
cout<< p[i][j]<<" ";
}
}
return 0;
}

【手动输入列数可以运行】
#include <iostream>
using namespace std;
int main()
{
int m, n;
cout << "请输入行数为:" << endl;
cin >> m;
int(*p)[3];
p = new int[m][3];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "请输入p[" << i << "][" << j << "]的值" << endl;
cin >> p[i][j];
}
}
cout << "输出为:" << endl;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
cout<< p[i][j]<<" ";
}
}
return 0;
}

【基本格式】
int (*p)[列]
p=new int[行][列]
边栏推荐
猜你喜欢
随机推荐
Shell:条件测试操作
正则表达式符号
pytest interface automation testing framework | skip test classes
Do I need to introduce any dependencies to write data to clickhouse using flinksql?
sql server, FULL mode, dbcc shrinkfile(2,1) can not shrink the transaction log, or the original size, why?
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
Microsoft Azure & NVIDIA IoT 开发者季 I|Azure IoT & NVIDIA Jetson 开发基础
Pod environment variables and initContainer
企业数据虚拟化综合指南
走进音视频的世界——mp3封装格式
【应用推荐】常见资源管理器整理,含个人使用体验和产品选型推荐
codeforces每日5题(均1600)-第二十七天
在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作
PerViT: 神经网络也能像人类利用外围视觉一样观察图像!
常见的API安全缺陷有哪些?
XX市消防救援指挥中心实战指挥平台多链路聚合解决方案实例
MySQL查询进阶——从函数到表连接的使用你还记得吗
CTO强烈禁止使用Calendar,那用啥?
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
GBase 8s 锁分类







