1std::stringstream test("this_is_a_test_string");
2std::string segment;
3std::vector<std::string> seglist;
4
5while(std::getline(test, segment, '_'))
6{
7 seglist.push_back(segment); //Spit string at '_' character
8}
1void tokenize(string &str, char delim, vector<string> &out)
2{
3 size_t start;
4 size_t end = 0;
5
6 while ((start = str.find_first_not_of(delim, end)) != string::npos)
7 {
8 end = str.find(delim, start);
9 out.push_back(str.substr(start, end - start));
10 }
11}
12
13int main()
14{
15 string s="a;b;c";
16 char d=';';
17 vector<string> a;
18 tokenize(s,d,a);
19 for(auto it:a) cout<<it<<" ";
20
21 return 0;
22}
1#include <vector>
2#include <string>
3#include <sstream>
4#include <iostream>
5using namespace std;
6
7int main()
8{
9 string s="i,love,my,country,very,much"; //declare a string
10 string answer[6]; // string array to store the result
11 stringstream string_stream(s); // creating string stream object
12 int i=0; // declaring i and assign to 0
13
14 while(string_stream.good()) // loop will continue if string stream is error free
15 {
16 string a;
17 getline( string_stream, a, ',' ); //calling getline fuction
18 answer[i]=a;
19 i++;
20 }
21
22 for(i=0;i<6;i++)
23 {
24 cout<<answer[i]<<endl; // printing a result
25 }
26 return 0;
27}