回答編集履歴
4
追記
test
CHANGED
@@ -101,3 +101,75 @@
|
|
101
101
|
*/
|
102
102
|
|
103
103
|
```
|
104
|
+
|
105
|
+
[追記] こっち↓の方がツブシが利くかなー...
|
106
|
+
|
107
|
+
```C++
|
108
|
+
|
109
|
+
template<typename String,typename Function>
|
110
|
+
|
111
|
+
void tokenize(const String& str, const String& delim, Function&& fun) {
|
112
|
+
|
113
|
+
for ( typename String::size_type spos, epos = 0;
|
114
|
+
|
115
|
+
(spos = str.find_first_not_of(delim, epos)) != String::npos;) {
|
116
|
+
|
117
|
+
fun(str.substr(spos,(epos = str.find_first_of(delim, spos))-spos));
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
/* おためし */
|
126
|
+
|
127
|
+
#include <iostream>
|
128
|
+
|
129
|
+
#include <string>
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
int main() {
|
134
|
+
|
135
|
+
using namespace std;
|
136
|
+
|
137
|
+
string input[] = {
|
138
|
+
|
139
|
+
"abc",
|
140
|
+
|
141
|
+
"a bb ccc",
|
142
|
+
|
143
|
+
" a bb ccc",
|
144
|
+
|
145
|
+
"a bb ccc ",
|
146
|
+
|
147
|
+
" a bb ccc dddd ",
|
148
|
+
|
149
|
+
" ",
|
150
|
+
|
151
|
+
""
|
152
|
+
|
153
|
+
};
|
154
|
+
|
155
|
+
for ( const auto& item : input ) {
|
156
|
+
|
157
|
+
using namespace std::literals::string_literals;
|
158
|
+
|
159
|
+
cout << '[' << item << "] -> \n ";
|
160
|
+
|
161
|
+
int count = 0;
|
162
|
+
|
163
|
+
auto print = [&](const string& token) { cout << ++count << '[' << token << "] ";};
|
164
|
+
|
165
|
+
tokenize(item, " "s, print);
|
166
|
+
|
167
|
+
cout << endl;
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
```
|
3
修正
test
CHANGED
@@ -8,13 +8,13 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
template<typename OutputIterator>
|
11
|
+
template<typename String,typename OutputIterator>
|
12
12
|
|
13
|
-
OutputIterator tokenize(const
|
13
|
+
OutputIterator tokenize(const String& str, const String& delim, OutputIterator out) {
|
14
14
|
|
15
|
-
|
15
|
+
for ( typename String::size_type spos, epos = 0;
|
16
16
|
|
17
|
-
|
17
|
+
(spos = str.find_first_not_of(delim, epos)) != String::npos;) {
|
18
18
|
|
19
19
|
*out++ = str.substr(spos,(epos = str.find_first_of(delim, spos))-spos);
|
20
20
|
|
@@ -38,6 +38,8 @@
|
|
38
38
|
|
39
39
|
using namespace std;
|
40
40
|
|
41
|
+
|
42
|
+
|
41
43
|
vector<string> input = {
|
42
44
|
|
43
45
|
"abc",
|
@@ -56,15 +58,15 @@
|
|
56
58
|
|
57
59
|
};
|
58
60
|
|
59
|
-
|
61
|
+
for ( const auto& item : input ) {
|
60
62
|
|
61
|
-
|
63
|
+
using namespace std::literals::string_literals;
|
62
64
|
|
63
65
|
cout << '[' << item << "] -> ";
|
64
66
|
|
65
|
-
out
|
67
|
+
vector<string> out;
|
66
68
|
|
67
|
-
tokenize(item, " ", back_inserter(out));
|
69
|
+
tokenize(item, " "s, back_inserter(out));
|
68
70
|
|
69
71
|
for ( const auto& token : out ) {
|
70
72
|
|
2
修正
test
CHANGED
@@ -12,15 +12,11 @@
|
|
12
12
|
|
13
13
|
OutputIterator tokenize(const std::string& str, const std::string& delim, OutputIterator out) {
|
14
14
|
|
15
|
-
std::string::size_type pos = 0;
|
15
|
+
std::string::size_type spos, epos = 0;
|
16
16
|
|
17
|
-
std::string::s
|
17
|
+
while ( (spos = str.find_first_not_of(delim, epos)) != std::string::npos ) {
|
18
18
|
|
19
|
-
while ( (pos = str.find_first_not_of(delim, pos+len)) != std::string::npos ) {
|
20
|
-
|
21
|
-
|
19
|
+
*out++ = str.substr(spos,(epos = str.find_first_of(delim, spos))-spos);
|
22
|
-
|
23
|
-
*out++ = str.substr(pos,len);
|
24
20
|
|
25
21
|
}
|
26
22
|
|
1
修正
test
CHANGED
@@ -12,23 +12,15 @@
|
|
12
12
|
|
13
13
|
OutputIterator tokenize(const std::string& str, const std::string& delim, OutputIterator out) {
|
14
14
|
|
15
|
-
typedef std::pair<std::string::size_type, std::string::size_type> result_type;
|
16
|
-
|
17
15
|
std::string::size_type pos = 0;
|
18
16
|
|
19
|
-
std::string::size_type len;
|
17
|
+
std::string::size_type len = 0;
|
20
18
|
|
21
|
-
while ( true ) {
|
22
|
-
|
23
|
-
pos = str.find_first_not_of(delim, pos)
|
19
|
+
while ( (pos = str.find_first_not_of(delim, pos+len)) != std::string::npos ) {
|
24
|
-
|
25
|
-
if ( pos == std::string::npos ) break;
|
26
20
|
|
27
21
|
len = str.find_first_of(delim, pos) - pos;
|
28
22
|
|
29
23
|
*out++ = str.substr(pos,len);
|
30
|
-
|
31
|
-
pos += len;
|
32
24
|
|
33
25
|
}
|
34
26
|
|