当前位置:网站首页>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[行][列]
边栏推荐
猜你喜欢
随机推荐
Redis学习
Gethostbyname \ getaddrinfo DNS domain name IP address is not safe
XX市消防救援指挥中心实战指挥平台多链路聚合解决方案实例
PHP获取时间戳后写数据库的一个问题
Mysql database deployment and initialization steps
在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作
22 Grab the Seat 1 C.Grab the Seat (Geometry + Violence)
Introduction to ADAS
node 格式化时间的传统做法与高级做法(moment)
network basic learning
ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API
Detailed explanation of JVM runtime data area and JMM memory model
【应用推荐】常见资源管理器整理,含个人使用体验和产品选型推荐
静态Pod、Pod创建流程、容器资源限制
Chapters 6 and 7 of Huawei Deep Learning Course
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
【STM32】入门(二):跑马灯-GPIO端口输出控制
Pytest | skip module interface test automation framework
【杭电多校第四场 B题】最短路图+缩点dp
Static Pod, Pod Creation Process, Container Resource Limits



![ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API](/img/b3/0167c22f14b97eb0206696495af7b5.png)





