我必须读取一个总共有N行的文件input.txt。每行包含两个整数。具体来说,我正在读取一个文件,其中两个整数都是2 ^(line_index -1)。
int temp1, temp2;
std::vector<int> vec1, vec2;
std::fstream fh("input.txt", std::ios_base::in);
for (int i = 0; i < N; i++) {
fh >> temp1 >> temp2;
vec1.push_back(temp1);
vec2.push_back(temp2);
}
//first few lines of input are
//1 1
//2 2
//4 4
// . . .
//Line 31 should be: 2147483647 2147483647
//but my code read it as 2147483647 1073741824
//This is always the case for N>30
在第30行之后,正如您在上面的代码片段中看到的那样,文件读取变得很奇怪。我的代码有问题吗?或者我读取文件的方法排序限制了我可以输入的变量?
您正在达到整数限制,请参阅this question。不确定为什么要这样做,但如果要保存更大的值,则需要不同的数据类型。