(→123aaa) |
|||
12번째 줄: | 12번째 줄: | ||
int main() { | int main() { | ||
string | string line("aaa1bbb2ccc"); | ||
regex rx("[0-9]"); | regex rx("[0-9]"); | ||
sregex_token_iterator iter( | sregex_token_iterator iter(line.begin(), line.end(), rx, -1), end; | ||
vector<string> strs{iter, end}; | vector<string> strs{iter, end}; | ||
for (string s: strs) cout << s << ", "; // aaa, bbb, ccc, | for (string s: strs) cout << s << ", "; // aaa, bbb, ccc, |
2023년 9월 29일 (금) 16:49 판
1 개요
- C++ 문자열 split() 구현
- C++ 문자열 preg_split() 구현
2 aaa1bbb2ccc
C++
CPU
2.7s
MEM
177M
2.8s
Copy
#include <iostream>
#include <vector>
#include <regex>
using namespace std;
int main() {
string line("aaa1bbb2ccc");
regex rx("[0-9]");
sregex_token_iterator iter(line.begin(), line.end(), rx, -1), end;
vector<string> strs{iter, end};
for (string s: strs) cout << s << ", "; // aaa, bbb, ccc,
}
aaa, bbb, ccc,
3 123aaa
C++
Copy
#include <iostream>
#include <vector>
#include <regex>
using namespace std;
int main() {
string line("123aaa");
regex seps("[0-9]");
sregex_token_iterator iter(line.begin(), line.end(), seps, -1), end;
vector<string> tokens{iter, end};
for (string t: tokens) cout << t << ", "; // , , , aaa,
}
Loading
C++
Copy
#include <iostream>
#include <vector>
#include <regex>
using namespace std;
int main() {
string line("123aaa");
regex seps("[0-9]");
sregex_token_iterator iter(line.begin(), line.end(), seps, -1);
auto tokens = vector<string>(iter, sregex_token_iterator());
tokens.erase(remove_if(tokens.begin(), tokens.end(), [](string const& s){ return s.empty(); }), tokens.end());
for (string t: tokens) cout << t << ", "; // aaa,
}
Loading
4 abc,defgh,ijk
C++
Copy
#include <iostream>
#include <regex>
#include <vector>
using namespace std;
int main() {
string in = "abc,defgh,ijk";
regex rx(",");
sregex_token_iterator iter(in.begin(), in.end(), rx, -1), end;
vector<string> strs{iter, end};
for (string s: strs) cout << s << ' '; // abc defgh ijk
}
Loading
C++
Copy
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
string in = "abc,defgh,ijk";
vector<string> strs;
istringstream ss(in);
for (string s; getline(ss, s, ','); ) {
strs.push_back(s);
}
for (string s: strs) cout << s << ' '; // abc defgh ijk
}
Loading
5 같이 보기
편집자 Jmnote
로그인하시면 댓글을 쓸 수 있습니다.