0
我試圖初始化1.000.001
元素的數組,C++
像這樣:int array[1000001]
。我有4GB的內存,所以我猜想問題在於我的筆記本電腦不能容納這麼大的陣列,因爲它的尺寸是4 * 1000001 bytes
。所以我決定試試char
(只是因爲我想知道我的猜測是否正確)。我正在從文件讀取數組。這是我的代碼:從C++文件讀取的奇怪數組
#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數組? – NathanOliver