zoukankan      html  css  js  c++  java
  • C++标准库STL 之 我觉得应该有的方法——split

    C++标准库STL 之 我觉得应该有的方法——split

    vector<string> split(const string& s, const string& c) {
        vector<string> result;
        string::size_type pos1, pos2;
        pos2 = s.find(c);
        pos1 = 0;
        while (string::npos != pos2) {
            result.push_back(s.substr(pos1, pos2 - pos1));
            pos1 = pos2 + c.size();
            pos2 = s.find(c, pos1);
        }
        if (pos1 != s.length()) {
            result.push_back(s.substr(pos1));
        }
        return result;
    }

    使用示例

    #include <iostream>
    #include <string>
    #include <vector>
    
    int main()
    {
        string str = "Hello wooold ahh!";
        vector<string> v = split(str," ");
        for (string::size_type i = 0; i < v.size(); ++i) {
          cout << v[i] << endl;
        } 
        return 0;
    }

    输出

    Hello
    wooold
    ahh! 
  • 相关阅读:
    python安装
    实现node服务器
    VSCode集成tomcat及使用方法
    CommonJS规范
    Bootstrap 笔记
    vue笔记
    jgGrid模板添加
    vue组件中的data
    解析DNS
    StaticResource
  • 原文地址:https://www.cnblogs.com/skyeisgood/p/12487737.html
Copyright © 2011-2022 走看看