본문 바로가기

c++/Study

c++ StringStream 사용법

생각보다 심플하고, 아주아주 유용할거같다

기능 : 띄어쓰기, \n 줄바꿈 이 두개를 구분하게 해주는 아주 유용한 함수~~

 

1) 해더에는 sstream

2) 필요한 인풋들

- 나누고 싶은 string

- 나눠진걸 결과에 담을 string 배열

- 옮겨담기용 string 하나, int index 하나

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	string a = " gfd qwe asdd sdf sdf";

	stringstream ss(a);
	string result[5];
	string token;

	int index = 0;

	while (ss >> token)
		result[index++] = token;

	for (int i = 0; i < 5; i++)
	{
		cout << result[i] << " ";
	}

}

'c++ > Study' 카테고리의 다른 글

다익스트라(Dijkstra) 알고리즘 간단 정리  (0) 2021.10.04
이분 탐색  (0) 2021.06.24
알고리즘  (0) 2021.06.19
상속  (0) 2021.05.25
객체간 관계  (0) 2021.05.24