-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstring_split.cpp
80 lines (68 loc) · 2.06 KB
/
string_split.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
//https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
//https://www.geeksforgeeks.org/split-a-sentence-into-words-in-cpp/
//https://stackoverflow.com/questions/17807634/stringstream-duplicates-last-word
std::vector<std::string> string_split(std::string str, std::string delimiter){
size_t pos = 0;
std::string token;
std::vector<std::string> result;
//Style 1, need to deal with remaining string after the loop
/*
while ((pos = str.find(delimiter)) != std::string::npos) {
token = str.substr(0, pos);
result.push_back(token);
str.erase(0, pos + delimiter.length());
}
result.push_back(str);
*/
//Style 2, can deal with all tokens inside the while loop
while(true){
pos = str.find(delimiter);
//works even if pos is string::npos
token = str.substr(0, pos);
result.push_back(token);
if(pos == string::npos) break;
//pos+1 equals to 0, so the line below can't handle this situation
str.erase(0, pos+delimiter.length());
}
return result;
}
std::vector<std::string> string_split2(std::string str)
{
// Used to split string around spaces.
std::istringstream ss(str);
std::vector<std::string> result;
std::string word;
//need to check whether the reading success before using it
while(ss >> word){
result.push_back(word);
}
return result;
}
int main()
{
std::string str = "scott>=tiger>=mushroom";
std::string delimiter = ">=";
//Method 1
for(std::string token : string_split(str, delimiter)){
std::cout << token << std::endl;
}
std::cout << "=====================" << std::endl;
//Method 2
str = "Geeks for Geeks";
std::vector<std::string> result = string_split2(str);
std::copy(result.begin(), result.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
return 0;
}
/*
scott
tiger
mushroom
=====================
Geeks
for
Geeks
*/