📜  c ++ split_string - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:48.555000             🧑  作者: Mango

代码示例1
std::vector split_string(const std::string& str,
                                      const std::string& delimiter)
{
    std::vector strings;

    std::string::size_type pos = 0;
    std::string::size_type prev = 0;
    while ((pos = str.find(delimiter, prev)) != std::string::npos)
    {
        strings.push_back(str.substr(prev, pos - prev));
        prev = pos + 1;
    }

    // To get the last substring (or only, if delimiter is not found)
    strings.push_back(str.substr(prev));

    return strings;
}