亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何迭代字符串的單詞?

如何迭代字符串的單詞?

蕪湖不蕪 2019-05-20 17:38:12
我正在嘗試迭代字符串的單詞??梢约僭O該字符串由用空格分隔的單詞組成。請注意,我對C字符串函數或那種字符操作/訪問不感興趣。另外,請在答案中優先考慮優雅而不是效率。我現在最好的解決方案是:#include <iostream>#include <sstream>#include <string>using namespace std;int main(){     string s = "Somewhere down the road";     istringstream iss(s);     do     {         string subs;         iss >> subs;         cout << "Substring: " << subs << endl;     } while (iss);}有沒有更優雅的方式來做到這一點?
查看完整描述

4 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

#include <vector>

#include <string>

#include <sstream>


int main()

{

    std::string str("Split me by whitespaces");

    std::string buf;                 // Have a buffer string

    std::stringstream ss(str);       // Insert the string into a stream


    std::vector<std::string> tokens; // Create vector to hold our words


    while (ss >> buf)

        tokens.push_back(buf);


    return 0;

}


查看完整回答
反對 回復 2019-05-20
  • 4 回答
  • 0 關注
  • 656 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號