Search

stringstream 사용법

생성일
2023/02/15 07:00
태그
C++

stringstream 사용법

여러가지 자료형이 string으로 한줄에 들어오면 어떤 식으로 처리해야할까?
stringstream을 사용해, 한줄에 들어오면 파싱해서 용도에 맞게 사용한다.

문자열 스트림

stringstream 이란?

문자열에서 작동하는 스트림 클래스이다.
이 클래스 객체는 일련의 문자를 포함하는 문자열 버퍼를 사용하고 있다.
문자열에서 내가 원하는 자료형의 데이터를 추출할 때 사용한다.
입출력 스트림 : 입력 스트림, 출력 스트림을 모두 할 수 있다.

istringstream

입력 스트림
문자열을 공백과 ‘\n’을 기준으로 여러 개의 다른 형식으로 차례대로 분리할 때 편리하다.
반복문 실행 시, 자료형에 맞는 데이터가 없을 때까지 실행된다.

헤더 정보

#include <sstream>
C++
복사
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { istringstream iss("test\n123 aaa 456"); string s1, s2; int i1, i2; iss >> s1 >> i1 >> s2 >> i2; // 문자열을 파싱하고 변수형에 맞게 변환한다. cout << s1 << '\n'; // test cout << i1 << '\n'; // 123 cout << s2 << '\n'; // aaa cout << i2 << '\n'; // 456 return 0; }
C++
복사
#include <iostream> #include <sstream> #include <string> int main() { string str1 = "1D2S#10S"; string str2 = "1111DAWV2S#10S"; istringstream iss1(str1); istringstream iss2(str2); int num1, num2; while (iss1 >> num1) cout << num1 << " "; cout << '\n'; while (iss2 >> num2) cout << num2 << " "; cout << '\n'; istringstream iss3(str3); istringstream iss4(str4); char ch1, ch2; while (iss3 >> ch1) cout << ch1 << " "; cout << '\n'; while (iss4 >> ch2) cout << ch2 << " "; cout << '\n'; return 0; } // 실행 결과 // 1 // 1111 // 1 D 2 S # 1 0 S // 1 1 1 1 D A W V 2 S # 1 0 S
C++
복사

ostringstream

출력 스트림
문자열을 조립하거나 특정 형식을 문자열로 변환하기 위해 사용한다.
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { ostringstream oss; string s1 = "abc", s2 = "gjw"; int i1 = 19234; double d1 = 3.591; oss << s1 << '\n' << i1 << '\n' << s2 << '\n' << d1; // 문자열을 붙인다. cout << oss.str(); // 문자열을 꺼낸다. return 0; } // 실행 결과 // abc // 19234 // gjw // 3.591
C++
복사

str(), clear()

str(string s)
stringstream에 저장된 문자열을 바꾼다. 이때 s가 “”일 경우, 문자열을 삭제하는 것과 같다.
str()
stringstream이 저장하고 있는 문자열의 복사본을 반환한다.
clear()
stringstream 재사용하려면 clear()를 실행해야 한다. 이때 저장된 문자열이 삭제되진 않는다.

get(), unget()

get()
커서를 뒤로 옮기면서 값을 반환한다.
unget()
커서를 앞으로 다시 옮긴다.
#include <iostream> #include <sstream> using namespace std; int main() { string str = "123abc"; // get() stringstream ss1; ss1.str(str); cout << ss1.get()-'0'; // 1 cout << ss1.get()-'0'; // 2 // unget() stringstream ss2; ss2.str(str); char ch; ss2 >> ch; // 1 ss2 >> ch; // 2 ss2.unget(); ss2 >> ch; // 1 return 0; }
C++
복사

getline()

문자열을 공백이나 ‘\n’ 이 아닌 다른 문자를 기준으로 분리하고 싶을 때 사용한다.
#include <iostream> #include <sstream> using namespace std; int main() { string str = "gkg|qiew|789", token; stringstream ss(str); while (getline(ss, token, '|')) { cout << token << '\n'; } } // 실행 결과 // gkg // qiew // 789
C++
복사

예제

날짜를 초로 바꾸기

#include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; int main() { vector<long long> time; string str = "2019::06:30 12:00:30"; for (int i = 0; i < str.size(); i++) { if (str[i] == ':') str[i] = ' '; } long long num = 0; stringstream stream; stream.str(str); while (stream >> num) time.push_back(num); long long second = 0; second += time[0] * 365 * 24 * 60 * 60; // 연 second += time[1] * 30 * 24 * 60 * 60; // 월 second += time[2] * 24 * 60 * 60; // 일 second += time[3] * 60 * 60; // 시 second += time[4] * 60; // 분 second += time[5]; // 초 cout << second; } // 63689371230%
C++
복사

ref)