stream
다양한 타입의 데이터를 통합된 방식으로 다루며, 내부적으로는 문자열 버퍼나 바이트 버퍼로 자동 변환되기 때문에, 우리가 편하게 입·출력 할 수 있게 해주는 추상화 도구입니다.
c++에서의 표준 입력 스트림 객체는 cin이며, 표준 출력 스트림 객체는 cout입니다.
sstream은 C++의 표준 라이브러리에서 제공하는 문자열 스트림 관련 클래스를 포함하는 헤더 파일로, 문자열과의 입·출력을 가능하게 해줍니다.
일반적인 파일이나 콘솔 입출력과 비슷한 방식으로 문자열을 다룰 수 있도록 도와줍니다.
#include <sstream>
#include <iostream>
using namespace std;
int main() {
string input = "123 456";
istringstream iss(input);
int a, b;
iss >> a >> b;
cout << a << " " << b << endl;
return 0;
}
<결과값>
#include <sstream>
#include <iostream>
using namespace std;
int main() {
int num = 42;
ostringstream oss;
oss << "The number is " << num;
string result = oss.str(); // str() 함수는 oss에 저장된 문자열을 반환
cout << result;
return 0;
}
<결과값>
#include <sstream>
#include <iostream>
using namespace std;
int main() {
string data = "apple 42 banana 100 cherry 300";
stringstream ss(data);
string fruit;
int quantity;
while (ss >> fruit >> quantity) {
cout << "Fruit : " << fruit << " | Quantity : " << quantity << endl;
}
return 0;
}
<결과값>