'Need to split string into vector with delimiter

The following code is supposed to split a string with delimiters using a function.

Given the following function declaration:

vector<string> split(string target, string delimiter);

I need to implement the function so that it returns a vector of substrings in target that are separated by the string delimiter. For example:

split("10,20,30", ",")

should return a vector with the strings "10", "20", and "30". Similarly,

split("do re mi fa so la ti do", " ")

should return a vector with the strings "do", "re", "mi", "fa", "so", "la", "ti", and "do".

My code only outputs the first element of the vector. I don't see anything wrong with the function implementation.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> split(string, string);

int main()
{
    vector<string> v;
    string target;
    string delim;
    split(target, delim);
    v = split(target, delim);
    for (unsigned long i = 0; i < v.size(); i++)
        cout << v[i] << " ";
}

vector<string> split(string target, string delim)
{
    cout << "Enter string: "<< endl;
    getline(cin, target);
    cout << "Enter delimiter:" << endl;
    getline(cin, delim);
    vector<string> v;
    size_t x = target.find(delim);
    while (x!= string::npos) {
        v.push_back(target.substr(0,x));
        target = target.substr(x);
        x = target.find(delim);
    }
    return v;
}


Solution 1:[1]

Try something more like this instead:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> split(string, string);

int main()
{
    vector<string> v;
    string target;
    string delim;

    cout << "Enter string: "<< endl;
    getline(cin, target);
    cout << "Enter delimiter:" << endl;
    getline(cin, delim);

    v = split(target, delim);
    for (unsigned long i = 0; i < v.size(); i++)
        cout << v[i] << " ";
}

vector<string> split(string target, string delim)
{
    vector<string> v;
    if (!target.empty()) {
        string::size_type start = 0;
        do {
            size_t x = target.find(delim, start);
            if (x == string::npos)
                break;

            v.push_back(target.substr(start, x-start));
            start += delim.size();
        }
        while (true);

        v.push_back(target.substr(start));            
    }
    return v;
}

Solution 2:[2]

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> split(string target, string delimiter)
{
    vector<string> components;
    if (!target.empty())
    {
         size_t start = 0;
         do
         {
             const size_t index = target.find(delimiter, start);
             if (index == string::npos)
             {
                 break;
             }
             const size_t length = index - start;
             components.push_back(target.substr(start, length));
             start += (length + delimiter.size());
         } while (true);
         components.push_back(target.substr(start));
    }

    return components;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2