Implement the split() function to return a vector of strings

implement the function split() so that it returns a vector of the strings in target that are separated by the string delimiter. For example,
split("do,re,me,fa,so,la,ti,do", ",") should return a vector with the strings "do", "re", "me", "fa", "so", "la", "ti" and "do".
Test your function split() in a driver program that displays the strings in the vector after the target has been split.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    vector<string> split (string target, string delimiter);






}


I havn't really done vectors , so can anyone please assist me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <string>
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 delimiter);
{


 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;
}



Can anyone help me?
someone told me to change it a bit to the following



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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 delimiter)
{
    vector<string> v;
    if (!target.empty()) {
        string::size_type start = 0;
        do {
            size_t x = target.find(delimiter, start);
            if (x == string::npos)
                break;

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

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


It works
Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <string>

auto split(const std::string& str, const std::string& delim)
{
	std::vector<std::string> vs;
	size_t pos {};

	for (size_t fd = 0; (fd = str.find(delim, pos)) != std::string::npos; pos = fd + delim.size())
		vs.emplace_back(str.data() + pos, str.data() + fd);

	vs.emplace_back(str.data() + pos, str.data() + str.size());
	return vs;
}

int main()
{
	const auto vs {split("do,re,me,fa,so,la,ti,do", ",")};

	for (const auto& e : vs)
		std::cout << e << '\n';
}



do
re
me
fa
so
la
ti
do

Topic archived. No new replies allowed.