竞赛讨论区 > c++ cin.get() 来判断一行是否输入完毕
头像
听见_下雨的声音
发布于 2020-04-25 19:34
+ 关注

c++ cin.get() 来判断一行是否输入完毕

#include 
#include 

using namespace std;

int main()
{
    string tmp;
    vector res;
    while(cin >> tmp)  // 一直读
    {
        res.push_back(tmp);
        if(cin.get() == '\n') // 通过判断是否换行
        {
            sort(res.begin(),res.end());
            for(auto s : res)
                cout << s << " ";
            cout << endl; 
            res.clear();
        }
    }
    return 0;
}

stringstream参考 彼岸蒹葭

#include <iostream>
#include <sstream>
#include <algorithm>

using namespace std;

int main(){
    string str;
    while (getline(cin, str)){
        stringstream ss;
        ss << str;
        string s;
        vector<string> res;
        while (ss >> s){
            res.push_back(s);
        }

        sort(res.begin(), res.end());
        for(auto u : res)
            cout << u << " ";
        cout << endl;
    }
    return 0;
}

全部评论

(0) 回帖
加载中...
话题 回帖

等你来战

查看全部

热门推荐