- istringstream은 string을 입력받아 공백을 기준으로 변수 형식에 맞게 자르고 그 값들을 각각의 다른 변수들 선인 및 초기화시 사용할 수 있다.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class HumanBeing
{
private:
string myName;
string myBeVerb;
int myAge;
public:
HumanBeing(string name,string beVerb,int age)
:myName(name),myBeVerb(beVerb),myAge(age)
{
iAm();
}
void iAm ()
{
cout<<"My name is: "<<myName<<endl;
cout<<"My beverb is: "<<myBeVerb<<endl;
cout<<"My age is: "<<myAge<<endl;
}
};
int main()
{
istringstream is("kim is 25");
string name, beVerb;
int age;
is>>name>>beVerb>>age;
HumanBeing realKim(name,beVerb,age);
return 0;
}
- ostringstream은 str()을 이용하여 서로 다른 type들의 변수를 string type으로 변환 및 합칠 수 있다.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
ostringstream os;
string name = "kim";
string beVerb = "is";
int age = 25;
os<<name<<" "<<beVerb<<" "<<age;
cout<<os.str()<<endl;
return 0;
}
- stringstream은 문자열을 공백과 개행문자들을 기준으로 분리할 수 있다.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string me;
stringstream ss("Kim is 25");
while (ss>>me)
{
cout<<me<<endl;
}
return 0;
}