Monday, April 6, 2015

C++ stringstream 초기화

STL을 사용할때 buffer와 sprintf를 사용하는 대신에 stringstream을 사용하는데 이것의 사용법은 대충 알았었는데 초기화를 몰라서 얼마전에 찾았다. 아래와 같이 사용하고, 초기화를 하면 재사용을 할수 있다.

std::stringstream ss;
std::string data="1";

ss << "value:" << data;
// ss.str()를 하면 std::string으로 리턴된다.
OutputDebugStringA(ss.str().c_str());

//초기화. ss.clear()를 하면 초기화가 되지 않음
ss.str("");
//초기화 된 상태에서 다른 목적으로 재사용 가능
ss << ...

No comments:

Post a Comment