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;
}
}
输入:
10位
游客
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
订阅者
rowdark
tankengineer
对于所有其他输入(即使 n=10),我的代码运行良好,但是对于这个特定输入(上面提到的),它给出了分段错误。
您的代码不可能按原样编译,并且您正在使用C++ 不支持的VLA:s,因此很难重现您的问题。尝试通过使用 C++ 容器(如std::vector
, )来避免它。例子:
#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";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句