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; }