std::istreambuf_iterator<char> eos;
std::string s(std::istreambuf_iterator<char>(stream), eos);

----------------------------------------------------------------------------

(could be a one-liner if not for MVP)

post-2011 edit, this approach is now spelled

std::string s(std::istreambuf_iterator<char>(stream), {});
----------------------------------------------------------------------------

I'm late to the party, but here is a fairly efficient solution:

std::string gulp(std::istream &in)
{
std::string ret;
char buffer[4096];
while (in.read(buffer, sizeof(buffer)))
ret.append(buffer, sizeof(buffer));
ret.append(buffer, in.gcount());
return ret;
}

I did some benchmarking, and it turns out that the std::istreambuf_iterator technique (used by the accepted answer) is actually much slower. On gcc 4.4.5 with -O3, it's about a 4.5x difference on my machine, and the gap becomes wider with lower optimization settings.

----------------------------------------------------------------------------

You can try using something from algorithms. I have to get ready for work but here's a very quick stab at things (there's got to be a better way):

copy( istreambuf_iterator<char>(stream), istreambuf_iterator<char>(), back_inserter(s) );
----------------------------------------------------------------------------

You could do

std::string s;
std::ostringstream os;
os<<stream.rdbuf();
s=os.str();

but I don't know if it's more efficient.

Alternative version:

std::string s;
std::ostringstream os;
stream>>os.rdbuf();
s=os.str();
----------------------------------------------------------------------------

Well, if you are looking for a simple and 'readable' way to do it. I would recomend add/use some high level framework on your project. For that I's always use Poco and Boost on all my projects. In this case, with Poco:

    string text;
FileStream fstream(TEXT_FILE_PATH);
StreamCopier::copyToString(fstream, text);
----------------------------------------------------------------------------

Perhaps this 1 line C++11 solution:

std::vector<char> s{std::istreambuf_iterator<char>{in},{}};
 
 

c++ istream转换为std::string的更多相关文章

  1. QString, Std::string, char *相互转换

    Qt 库中对字符串类型进行了封装,QString 类提供了所有字符串操作方法,给开发带来了便利. 由于第三方库的类型基本上都是标准的类型,即使用std::string或char *来表示字符 (串) ...

  2. C++ 将 std::string 转换为 char*

    参考: std::string to char* C++ 将 std::string 转换为 char* 目前没有直接进行转换的方法.必须通过string对象的c_str()方法,获取C-style的 ...

  3. C++不存在从std::string转换为LPCWSTR的适当函数

    LPCWSTR是什么类型呢? 看看如何定义的: typedef const wchar_t* LPCWSTR; 顾名思义就是: LPCWSTR是一个指向unicode编码字符串的32位指针,所指向字符 ...

  4. VC++ 中使用 std::string 转换字符串编码

    目录 第1章说明    1 1.1 代码    1 1.2 使用    4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...

  5. c++ std::string 用法

    std::string用法总结 在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询.在网上摘抄一下总结一下,为以后的查询方便: string类的构造函数: string(co ...

  6. std::string 是什么

    #include "stdafx.h" #include <iostream> #include <string> using std::cout; usi ...

  7. std::string 用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  8. std::string find 的返回值

    std::string  的方法 find,返回值类型是std::string::size_type, 对应的是查找对象在字符串中的位置(从0开始), 如果未查找到,该返回值是一个很大的数据(4294 ...

  9. UTF8与std:string互转

    Ajax请求发送的UTF8编码字符串传到后台使用std:string进一步处理,如果包含中文会出现中文乱码的问题: 特找了一下转码的解决方法,直接代码如下:  C++ Code  1234567891 ...

随机推荐

  1. spring oxm入门(包含demo)

    O/X Mapper 是什么? Spring 3.0 的一个新特性是 O/X Mapper.O/X 映射器这个概念并不新鲜,O 代表 Object,X 代表 XML.它的目的是在 Java 对象(几乎 ...

  2. 多目标线性规划求解方法及matlab实现

    转载: https://blog.csdn.net/wzl1997/article/details/79120323

  3. iOS:当点击 FormSheet 之外时,关闭该视图

    @interface XXViewController (){ @property (strong, nonatomic) UITapGestureRecognizer *tapGesture; - ...

  4. What really happens when you navigate to a URL

    As a software developer, you certainly have a high-level picture of how web apps work and what kinds ...

  5. ubuntu 12.04和Windows 7双系统的安装方法

    ubuntu是一个操作系统,和安卓一样同是linux内核下的子民,所以来这贡献一下,也求JW放出安卓4.0原生正式版,我只想要这个. 前几天发布了ubuntu12.04LTS 版本,兴奋ing. 看到 ...

  6. SQL2000系统表、存储过程、函数的功能介绍及应用

    转自:http://blog.csdn.net/zlp321002/article/details/480925 ----系统表------------------------------------ ...

  7. Microsoft Word 2010/2013 无法创建工作文件 请检查临时环境变量

    解决方案:重置IE缓存文件夹

  8. Single Pattern(单例模式)

    单例模式是一种常用的软件设计模式.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源.如果希望在系统中某个类的实例只能存在一个,单例模式是最好的 ...

  9. 简要介绍DES、RSA MD5 sha1 四种加密算法的优缺点,以及使用场合

    美国数据加密标准(DES)是对称密码算法,就是加密密钥能够从解密密钥中推算出来,反过来也成立.密钥较短,加密处理简单,加解密速度快,适用于加密大量数据的场合.RSA是非对称算法,加密密钥和解密密钥是不 ...

  10. GitHub 二次验证收不到短信咋办?

    身在天朝,用了国外的代码托管服务,会有些烦恼的. 网速慢就不说了,如果启用了二次验证,短信收不到那就悲催了. 之前的都能收到短信的,突然间尝试了很多天都不行,联系github的客服,几次的答复如下: ...