第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何迭代字符串的單詞?

如何迭代字符串的單詞?

蕪湖不蕪 2019-05-20 17:38:12
我正在嘗試迭代字符串的單詞??梢约僭O(shè)該字符串由用空格分隔的單詞組成。請注意,我對C字符串函數(shù)或那種字符操作/訪問不感興趣。另外,請在答案中優(yōu)先考慮優(yōu)雅而不是效率。我現(xiàn)在最好的解決方案是:#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);}有沒有更優(yōu)雅的方式來做到這一點?
查看完整描述

4 回答

?
慕村9548890

TA貢獻1884條經(jīng)驗 獲得超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 關(guān)注
  • 642 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號