"C++ preg split() 구현"의 두 판 사이의 차이

31번째 줄: 31번째 줄:
     sregex_token_iterator iter(in.begin(), in.end(), rx, -1), end;
     sregex_token_iterator iter(in.begin(), in.end(), rx, -1), end;
     vector<string> strs{iter, end};
     vector<string> strs{iter, end};
     for (string s: strs) cout << s << ", "; //
     for (string s: strs) cout << s << ", "; // , , , aaa,
}
}
</syntaxhighlight>
</syntaxhighlight>

2023년 9월 29일 (금) 16:31 판

1 개요

C++ 문자열 split() 구현
C++ 문자열 preg_split() 구현
C++
Copy
#include <iostream>
#include <vector>
#include <regex>
using namespace std;

int main() {
    string in = "aaa1bbb2ccc";
    
    regex rx("[0-9]");
    sregex_token_iterator iter(in.begin(), in.end(), rx, -1), end;
    vector<string> strs{iter, end};
    for (string s: strs) cout << s << ", "; // aaa, bbb, ccc, 
}
Loading
C++
Copy
#include <iostream>
#include <vector>
#include <regex>
using namespace std;

int main() {
    string in = "123aaa";
    
    regex rx("[0-9]");
    sregex_token_iterator iter(in.begin(), in.end(), rx, -1), end;
    vector<string> strs{iter, end};
    for (string s: strs) cout << s << ", "; // , , , aaa, 
}
Loading
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 같이 보기