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;
}
全部评论

相关推荐

头像
今天 10:53
已编辑
东北大学 自动化类
点赞 评论 收藏
转发
点赞 1 评论
分享
牛客网
牛客企业服务