C++ 为什么下面的C++;给出此特定输入的分段错误的代码? intmain(){ int n; 中国 cin.ignore(32767,“\n”); 字符串arr[n],温度; 对于(int i=0;i

C++ 为什么下面的C++;给出此特定输入的分段错误的代码? intmain(){ int n; 中国 cin.ignore(32767,“\n”); 字符串arr[n],温度; 对于(int i=0;i,c++,segmentation-fault,C++,Segmentation Fault,),您的代码不能按原样编译,而您使用的是C++不支持的,因此很难再现您的问题。请尝试使用C++容器避免它,例如 STD::vector < /C> >。 int main(){ int n; cin>>n cin.ignore(32767,'\n'); string arr[n],temp; for(int i=0;i<n;i++){ getline(cin,temp); arr[i]=temp;

),您的代码不能按原样编译,而您使用的是C++不支持的,因此很难再现您的问题。请尝试使用C++容器避免它,例如<代码> STD::vector < /C> >。
int main(){
    int n;
    cin>>n
    cin.ignore(32767,'\n');
    string arr[n],temp;
    for(int i=0;i<n;i++){
        getline(cin,temp);
        arr[i]=temp;
    }
}
#包括
#包括
int main(){
int n;
标准:cin>>n;
std::cin.ignore();//丢弃仍在缓冲区中的“\n”
/声明一个标准的C++容器,就像字符串的向量
std::向量arr(n);

对于(int i=0;iWHORE <代码> < < /Cord>)声明,也注意到可变长度数组不是标准C++,而是编译器扩展。这是否编译?这是非标准C++,因为VLAs没有被C++标准支持。如其他所说,VLAs不被C++标准支持。使用动态数组或某种容器(例如,
vector
)。无法编译代码,因为[i]=temp而不是arr[i]=temp,所以您确定代码会崩溃吗?我看到的唯一一件事可能是您不希望getline第一次返回空行,因为它将在10之后读取\n
#include <iostream>
#include <vector>

int main() {
    int n;
    std::cin >> n;
    std::cin.ignore(); // discard the '\n' still in the buffer

    // declare a standard C++ container, like a vector of strings
    std::vector<std::string> arr(n);

    for(int i=0; i<n; ++i) {
        std::getline(std::cin, arr[i]);
    }

    std::cout << "VALUES:\n";
    for(auto& s : arr) {
        std::cout << s << "\n";
    }
}