C++에서는 stringstream을 사용하여 문자열을 공백 또는 '\n'을 기준으로 다양하게 자를 수 있다.
두가지 예시를 보면 다음과 같다.
<첫번째 예시>
#include<iostream>
using namespace std;
#include<string>
#include<sstream>
int main()
{
int num;
string str = "123\n45 78 999";
stringstream stream(str);
while(stream >> num){
cout << num << endl;
}
return 0;
}
--output--
123
45
78
999
<두번째 예시>
#include<iostream>
using namespace std;
#include<string>
#include<sstream>
int main()
{
string s;
string str = "this is the example of stringstream ";
stringstream stream(str);
while(stream >> s){
cout << s << endl;
}
return 0;
}
--output--
this
is
the
example
of
stringstream