C++一行中有空格的字符串输入

C++一行中有空格的字符串输入,第1张

草了个DJ,怎么一直记不住这个啊!

#include
#include
using namespace std;

int main(){
    string str;
    getline(cin, str);
    string x;
    stringstream ss(str);
    while(ss >> x){
        cout << x << endl;
    }
}

也可以这样:

#include 
#include
using namespace std;

int main () {
    vector<string> strVector;
    string tmp;
    while(cin>>tmp){
        strVector.push_back(tmp);
        if(cin.get()=='\n'){
        	// do something
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/867541.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-12
下一篇2022-05-12

发表评论

登录后才能评论

评论列表(0条)

    保存