C++에서는 문자열을 분리하는 함수가 따로 없어서 직접 구현해야 한다.
코드는 아래와 같다.
#include<iostream>
#include<sstream>
using namespace std;
int main() {
string str = "Hello World!"; //구분할 문자열
istringstream ss(str); //istringstream에 저장
string buffer; //분리된 문자열을 담는 변수
while (getline(ss, buffer, ' ')) { //' '을 기준으로 분리 후 출력
cout << buffer << '\n';
}
return 0;
}
'알고리즘 > C++' 카테고리의 다른 글
[알고리즘] 백트레킹 (0) | 2023.10.05 |
---|---|
[알고리즘] 다익스트라 (0) | 2023.10.02 |
[알고리즘] 이분 탐색 (0) | 2023.10.01 |
[알고리즘] 탐욕(그리디) 알고리즘 (0) | 2021.11.01 |
[알고리즘] 병합 정렬 - C++ (0) | 2021.09.22 |
댓글