美文网首页
在线笔试多行输入问题

在线笔试多行输入问题

作者: 小雨启明 | 来源:发表于2018-09-07 21:31 被阅读0次

针对多行输入问题

例如:
abcd
4
a
ab
abc
abcd
关键在于

cin.ignore();

原因:https://blog.csdn.net/iamiman/article/details/53468250
处理cin>>n时 换行符停留在缓冲中的问题,控制台已实测。

#include<string>
#include<iostream>
#using namespace std;
int main() {
    string str;
    vector<string> dict;
    getline(cin, str);

    int n;
    cin>>n;
    cin.ignore();

    string temp;
    for (int i = 0; i < n; i++) {
        getline(cin, temp);
        dict.push_back(temp);
    }
}

相关文章

网友评论

      本文标题:在线笔试多行输入问题

      本文链接:https://www.haomeiwen.com/subject/wtehgftx.html