-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_000_split.cpp
More file actions
33 lines (31 loc) · 1.09 KB
/
str_000_split.cpp
File metadata and controls
33 lines (31 loc) · 1.09 KB
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
// split関数 (1文字バージョン。指定された文字で文字列を分割)
vector<string> split(string s, char c) {
vector<string> ret;
string temp;
replace(s.begin(), s.end(), c, ' ');
stringstream ss(s);
while(ss >> temp) ret.pb(temp);
return ret;
}
// split関数 (1文字・ delimiter を引数に指定しないバージョン)
// ※ delimiter が複数あればその都度 replace 関数を増やす(でも遅くなるかも)
vector<string> split(string s) {
vector<string> ret;
string temp;
replace(s.begin(), s.end(), ',', ' ');
replace(s.begin(), s.end(), '/', ' ');
stringstream ss(s);
while(ss >> temp) ret.pb(temp);
return ret;
}
// split関数 (2文字以上バージョン。指定された文字列で文字列を分割)
vector<string> split(string s, string c) {
vector<string> ret;
string temp;
for(int pos = s.find(c); pos != string::npos; pos = s.find(c, pos)) {
s.replace(pos, c.size(), " ");
}
stringstream ss(s);
while(ss >> temp) ret.pb(temp);
return ret;
}