stringstream 기본용법
· 배운 것 · 2분 읽기
사용법 코드
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream os;
//흘려주는것, 뒤에다 그냥 붙임, insert생각하면됨
//endl도 들어감
os << "hello world"<< endl; // << insertion operator, >>extration operator
//앞에있는걸 지우고 이걸로 바꾸는것
os.str("hello world \n");
cout << os.str() << endl;
//extraction, 빈칸 자름
string str;
os >> str;
cout << str << endl;
//다가져옴
str = os.str();
cout << str << endl;
return 0;
}
의문점 ( 아직 잘 모르겠음 질문만 올려둠 )
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream os;
int i = 12345;
double d = 67.89;
os << i << ' ' << d;
string str1;
string str2;
//이때는 제대로 받아옴
os >> str1 >> str2;
cout << str1 << ' ' << str2 << endl;
i = 56789;
d = 12.34;
os.str("");
//이때 os가 값을 받아오지 못함
os << i << ' ' << d;
os.str("56789 12.34");
str1.clear(); str2.clear();
//이때 str1, str2가 값은 받아오지 못함
os >> str1 >> str2;
cout << str1 << ' ' << str2 << endl;
return 0;
}