在C++中实现分割函数

2022-05-08

以下示例是关于Cpp中包含在C++中实现分割函数用法的示例代码,想了解在C++中实现分割函数的具体用法?在C++中实现分割函数怎么用?在C++中实现分割函数使用的例子?那么可以参考以下相关源代码片段来学习它的具体使用方法。

[英]:implementing split function in c++源码类型:Cpp
// splits a std::string into vector<string> at a delimiter
vector<string> split(string x, char delim = ' ')
{
    x += delim; //includes a delimiter at the end so last word is also read
    vector<string> splitted;
    string temp = "";
    for (int i = 0; i < x.length(); i++)
    {
        if (x[i] == delim)
        {
            splitted.push_back(temp); //store words in "splitted" vector
            temp = "";
            i++;
        }
        temp += x[i];
    }
    return splitted;
}

本文地址:https://www.itbaoku.cn/snippets/785467.html