c++ - 数组的存储大小未知
问题描述
我想创建程序来反转数组。但是我收到一个错误,即数组a[]
的存储大小未知。
#include<iostream>
using namespace std;
int main()
{
int a[];
int b, c;
cin >> b;
for (int i = 0; i < b; i++)
cin >> a[i];
for (c = b; c >= 0; c--)
cout << a[c] << endl;
return 0;
}
解决方案
我想创建程序来反转数组
只需使用std::vector即可。
一个好的开始是:
#include <iostream>
#include <vector>
int main()
{
std::size_t size;
std::cin >> size; // size of the array
std::vector<int> a(size);// allocate memory for provided size and
// initialize with 0's
// using range based loop iterate though referances of
// each elements in the array and update with user input.
for (int& element : a) std::cin >> element;
return 0;
}
推荐阅读
- python - 如何将 Boost.Python 中的 map_indexing_suite 与自定义非标准对象一起使用?
- webrtc - jitsi 会议截图
- java - JWT 验证失败:无法为 RSA 签名指定密钥字节。请指定 PublicKey 或 PrivateKey 实例
- javascript - 如何使用jquery在循环中更改每个按钮的值
- entity-framework-core - 实体框架核心排除 INNER JOIN
- c# - 封装没有 lambda 表达式的字段
- c# - 如何使用 C# 中的 zkemkeeper 库在图片框中的运行时获取指纹?
- python - 根据熊猫组比较日期
- machine-learning - TFF 联邦学习,评估方法
- ios - 自定义委托以使用 UIView 类中的 pushViewController