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. C# byte[]保存成文件

    string path = Server.MapPath(@"\a.pdf"); FileStream fs = new FileStream(path, FileMode.Cre ...

  2. mysql用户管理 常用sql语句 mysql数据库备份恢复

     

  3. php无限分类二

    1.读取数据数据 /** * 所有分类数据 * */ public function categoryData(){ $order = 'id,sort asc'; $res = $this-> ...

  4. Thinkphp5笔记八:路由别名Route

    主要作用:隐藏自己的真实路由名称 application/Route.php 使用方法一: <?php use think\Route; Route::alias('home','index/i ...

  5. nginx配置ThinkPHP配置

    配置时注意红色部分 server { listen 8888 default_server; #listen [::]:80 default_server; server_name _; root / ...

  6. QT QTransform与QMatrix 有啥区别?

    刚开始学习QT,我使用的是QT5.12进行开发,要不时地查阅QT的官方帮助文档~ 仔细阅读QT官方帮助QTransform类以及QMatrix类,发现两个类的作用描述一模一样(“The QTransf ...

  7. 消息中间件的意义和应用场景 (activeMq)

    消息中间件一般两个功能,解耦和异步处理,分别举个例子吧 解耦合:比如我们做一个微博产品中的好友系统,就很需要使用消息中间件当我们添加一个关注的时候, 涉及以下几个子系统 推荐系统,需要根据你关注的人给 ...

  8. mysql中,创建表的时候指定if not exists参数的作用?

    需求说明: 在创建表的时候,如果指定if not exists语句,有什么作用,在此做个实验,并且官方手册, 理解下这个参数的作用. 操作过程: 1.创建测试表test01 mysql> cre ...

  9. struts2 第一次使用 404 页面引发的一系列问题

    环境:ubuntu  . eclipse.struts-2.3.24 问题描写叙述: 1.  struts2 訪问出现404 2.  严重: Exception starting filter Str ...

  10. Eclipse------导入项目后出现Java compiler level does not match the version of the installed Java project facet

    报错信息:Java compiler level does not match the version of the installed Java project facet 解决方法: 1.点击工具 ...