c++ - 有没有内置的方法在 C++ 中拆分字符串?

标签 c++ string split

嗯,有吗?字符串我的意思是 std::string

最佳答案

这是我使用的 perl 风格的拆分函数:

void split(const string& str, const string& delimiters , vector<string>& tokens)
{
    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}

关于c++ - 有没有内置的方法在 C++ 中拆分字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/599989/

相关文章:

c++ - 如何使用 OpenCV 检测视频中的方 block ?

r - 检查变量是否具有值 ''

r - 如何匹配R中两列之间的字符串?

c++ - 添加该行的所有总和

javascript - 在字符串/cookie 中的特定单词之前抓取字符

BASH - 根据条件将文件拆分为多个文件

c++ - 使用 QProgressBar : Cannot create children for a parent that is in a different thread 时

c++ - C++ 中的运行时模板依赖

c++ - 复制构造函数中的内存泄漏

c - 有没有办法将字符串数组拆分为 token 上的字符串子数组