c++字串split 函數實現

來源:互聯網
上載者:User

標籤:find   返回   輸入   ret   函數傳回值   字串分割   title   str   字串   

- 經常遇到字串分割問題,但是相對於c++而言實現比較麻煩,直接遍曆一遍也很冗餘

- 另外也適用於,在字串中找到某個字元的所有位置

//函數功能:將輸入字串s,以字串c(;)進行拆分,拆分結果放在v中//函數參數說明:s為輸入字串;c為拆分的字串;v為拆分結果//函數傳回值:正常返回0int split_string(const std::string& s, std::vector<std::string>& v, const std::string& c){    std::string::size_type pos1, pos2;    pos2 = s.find(c);    pos1 = 0;    while (std::string::npos != pos2)    {        v.push_back(s.substr(pos1, pos2 - pos1));        pos1 = pos2 + c.size();        pos2 = s.find(c, pos1);    }    if (pos1 != s.length())        v.push_back(s.substr(pos1));    return 0;}
  • 幾種c++字串split 函數實現的比較

c++字串split 函數實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.