Answers for "when you split a line in c++"

C++
0

implementing split function in c++

// 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;
}
Posted by: Guest on June-23-2021
0

c ++ splitlines

std::vector<std::string> Loader::StringToLines(std::string string)
{
    std::vector<std::string> result;
    std::string temp;
    int markbegin = 0;
    int markend = 0;

    for (int i = 0; i < string.length(); ++i) {     
        if (string[i] == 'n') {
            markend = i;
            result.push_back(string.substr(markbegin, markend - markbegin));
            markbegin = (i + 1);
        }
    }
    return result;
}
Posted by: Guest on August-12-2021

Browse Popular Code Answers by Language