C++ string split - C++

"Format"

Posted by Yongmin on September 13, 2021

용도

입력이 예를 들어 “hello world”라고 주어졌을 때, hello와 wolrd를 따로 입력을 받고 싶을 때 사용하는 코드이다.

소스 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<string>
#include<vector>
#include<sstream>

using namespace std;

int main()
{
    string str="hello world";
    istringstream ss(str);
    string stringBuffer;
    vector<string> v;

    while (getline(ss, stringBuffer, ' ')){
        v.push_back(stringBuffer);
        cout<<stringBuffer<<" ";
    }
    
    return 0;
}


# # #