-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_split.cpp
More file actions
61 lines (46 loc) · 1.52 KB
/
str_split.cpp
File metadata and controls
61 lines (46 loc) · 1.52 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
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
#include <bits/stdc++.h>
using namespace std;
// 回数制限なし
void str_split(string str, string sub_str, vector<string> &splited_strings) {
int ss_len, ss_point;
ss_len = sub_str.size();
// 部分文字列がヒットし無くなるまで無限ループ
while (true) {
ss_point = str.find(sub_str);
// 部分文字列が見つからなかった場合
if (ss_point == -1) {
break;
}
// 部分文字列が見つかった場合
else {
splited_strings.push_back(str.substr(0, ss_point));
str = str.substr(ss_point + ss_len, str.size() - ss_point - 1);
}
}
// 分割後の最後 or 分割文字列が見つからなかった時の処理
splited_strings.push_back(str.substr(0, str.size()));
return;
}
// 回数制限あり
void str_split(string str, string sub_str, int max,
vector<string> &splited_strings) {
int counter = 0, ss_len, ss_point;
ss_len = sub_str.size();
// 部分文字列がヒットし無くなるまで無限ループ
while (counter < max) {
ss_point = str.find(sub_str);
// 部分文字列が見つからなかった場合
if (ss_point == -1) {
break;
}
// 部分文字列が見つかった場合
else {
splited_strings.push_back(str.substr(0, ss_point));
str = str.substr(ss_point + ss_len, str.size() - ss_point - 1);
}
counter += 1;
}
// 分割後の最後 or 分割文字列が見つからなかった時の処理
splited_strings.push_back(str.substr(0, str.size()));
return;
}