C++ preg split() 구현

Jmnote (토론 | 기여)님의 2023년 9월 24일 (일) 14:46 판 (→‎개요)

1 개요

C++ 문자열 split() 구현
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

2 같이 보기