简体   繁体   English

接受输入时出现分段错误

[英]Segmentation Fault while accepting input

I am trying to accept the input from user where first line will be Integer to indicate number of testcases我正在尝试接受来自用户的输入,其中第一行将是 Integer 以指示测试用例的数量

if number is 3如果数字是 3

Input will be like输入会像

3
Hello world
hey there, I am John
have a nice day

I am using getline to read the input我正在使用getline读取输入

My code我的代码

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    int n;
    cin >> n;

    vector<string> arr;

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

Error:错误:

3 

Segmentation fault(core dumped)

arr is an empty vector, so arr[i] = s; arr是一个空向量,所以arr[i] = s; is going to access out of bounds.将访问越界。 The [] operator does not grow the vector. []运算符不会增大向量。 It can only be used to access already existing elements.它只能用于访问已经存在的元素。

You can't create an element of a vector using the [] indexing operator;不能使用[]索引运算符创建向量的元素; your line arr[i] = s;你的行arr[i] = s; is trying to assign a string to an element that doesn't (yet) exist .正试图将一个字符串分配给一个不(还)存在的元素。

There are several ways around this: first, you could use the push_back function to add a new element to the end of the vector, in each loop;有几种方法可以解决这个问题:首先,您可以使用push_back函数在每个循环中向向量的末尾添加一个新元素; second, you could use the resize member to pre-allocate a specified number of elements (which you can then use in the arr[i] = s; line);其次,您可以使用resize成员预先分配指定数量的元素(然后您可以在arr[i] = s;行中使用); third - and perhaps simplest - you can 'pre-allocate' the elements of the vector by specifying the number of elements in the declaration (constructor), like this:第三个——也许是最简单的——你可以通过在声明(构造函数)中指定元素的数量来“预分配”向量的元素,如下所示:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string> // Need this for the "getline()" function definition

using namespace std;

int main()
{
    size_t n; // Indexes and operations on std::vector use "size_t" rather than "int"
    cin >> n;
    cin.ignore(1); // Without this, there will be a leftover newline in the "cin" stream
//  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // More precise, actually!
    vector<string> arr(n);
//  arr.resize(n); // Alternative to using 'n' in constructor
    for (size_t i = 0; i < n; i++) {
        string s;
        getline(cin, s);
        arr[i] = s;
    }

    for (auto s : arr) cout << s << endl; // Just to confirm the input values
    return 0;
}

There are also a few other issues in your code that I have 'fixed' and commented on in the code I posted.您的代码中还有一些其他问题,我已经“修复”并在我发布的代码中进行了评论。 Feel free to ask for further clarification and/or explanation.随时要求进一步澄清和/或解释。


EDIT: On the use of the cin.ignore(1);编辑:关于cin.ignore(1); line I added, see Why does std::getline() skip input after a formatted extraction?我添加的行,请参阅为什么 std::getline() 在格式化提取后跳过输入? (and the excellent answers given there) for more details. (以及那里给出的优秀答案)了解更多详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM