【发布时间】:2017-11-16 18:19:17
【问题描述】:
我试图在C++ 中初始化1.000.001 元素的数组,如下所示:int array[1000001]。我有 4GB 的 RAM,所以我猜问题是我的笔记本电脑无法容纳这么大的阵列,因为它的大小为 4 * 1000001 bytes。所以我决定尝试让它char(只是因为我想知道我的猜测是否正确)。我正在从文件中读取数组。这是我的代码:
#include <iostream>
#include <fstream>
#include <climits>
using namespace std;
int main()
{
fstream in("C:\\Users\\HP\\Documents\\Visual Studio 2017\\Projects\\inputFile.in");
if (!in)
{
cerr << "Can't open input file\n";
return 1;
}
fstream out("outputFile.out", fstream::out);
if (!out)
{
cerr << "Can't open output file\n";
return 1;
}
int n;
in >> n;
int i;
char array[100];
for (i = 0; i < n; i++)
in >> array[i];
in.close();
out.close();
}
输入:5
45 5 4 3 12
我的数组是{4, 5, 5, 4, 3}。
对于输入:
5
12 3 4 5 45
我的数组是{1, 2, 3, 4, 5}
现在我真的很困惑。为什么会这样?
【问题讨论】:
-
为什么要将整数读入 char 数组?