[C++] 개념 - stringstream이란

영구·2024년 1월 24일

c++ 개념

목록 보기
2/10

[C++] stringstream

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

0개의 댓글