【c++】istream 转为 string

一直局限于使用cout、cin,在平时的开发中已经足够了。突然遇到istream,还真不知所措,在使用boost中,各种stream流被虐,今天一个需求就是把istream转为string,如下:

std::shared_ptr<boost::asio::streambuf> read_buffer(new boost::asio::streambuf);
......//字符串
std::istream stream(read_buffer.get());
std::istreambuf_iterator<char> eos;
std::string str(std::istreambuf_iterator<char>(stream), eos);

参考文章:http://stackoverflow.com/questions/3203452/how-to-read-entire-stream-into-a-stdstring

你可能感兴趣的:(C++-C)