首页 > 解决方案 > c ++中文件输入输出中的long long v / s int

问题描述

我尝试了许多不同的不成功方法,我将在下面描述最后一种方法-

#include<iostream>
#include<fstream>

//functions here


using namespace std;

int main(){
    int n;
    long long A[300001];
    ifstream myfile("sort.in");
    myfile>>n;
    
    for(int i=0;i<n;++i){
        myfile>>A[i];
    }
    cout<<A[0];
    myfile.close();
    
    return 0;
}

这行不通。

而下面的一个有效 -

#include<iostream>
#include<fstream>

//functions here


using namespace std;

int main(){
    int n;
    int A[300001];
    ifstream myfile("sort.in");
    myfile>>n;
    
    for(int i=0;i<n;++i){
        myfile>>A[i];
    }
    cout<<A[0];
    myfile.close();
    
    return 0;
}

问题是什么?

唯一的区别是使用 int 而不是 long long。怎么会有这么大的不同?顺便说一句,不工作是指它没有在屏幕上产生任何输出。

标签: c++

解决方案


这很可能是由大型静态定义的数组引起的堆栈溢出问题

代替

long long A[300001];

利用

std::vector<long long> A(300001);

推荐阅读