c++ istream转换为std::string
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的更多相关文章
- QString, Std::string, char *相互转换
Qt 库中对字符串类型进行了封装,QString 类提供了所有字符串操作方法,给开发带来了便利. 由于第三方库的类型基本上都是标准的类型,即使用std::string或char *来表示字符 (串) ...
- C++ 将 std::string 转换为 char*
参考: std::string to char* C++ 将 std::string 转换为 char* 目前没有直接进行转换的方法.必须通过string对象的c_str()方法,获取C-style的 ...
- C++不存在从std::string转换为LPCWSTR的适当函数
LPCWSTR是什么类型呢? 看看如何定义的: typedef const wchar_t* LPCWSTR; 顾名思义就是: LPCWSTR是一个指向unicode编码字符串的32位指针,所指向字符 ...
- VC++ 中使用 std::string 转换字符串编码
目录 第1章说明 1 1.1 代码 1 1.2 使用 4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...
- c++ std::string 用法
std::string用法总结 在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询.在网上摘抄一下总结一下,为以后的查询方便: string类的构造函数: string(co ...
- std::string 是什么
#include "stdafx.h" #include <iostream> #include <string> using std::cout; usi ...
- std::string 用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- std::string find 的返回值
std::string 的方法 find,返回值类型是std::string::size_type, 对应的是查找对象在字符串中的位置(从0开始), 如果未查找到,该返回值是一个很大的数据(4294 ...
- UTF8与std:string互转
Ajax请求发送的UTF8编码字符串传到后台使用std:string进一步处理,如果包含中文会出现中文乱码的问题: 特找了一下转码的解决方法,直接代码如下: C++ Code 1234567891 ...
随机推荐
- utf8_unicode_ci与utf8_general_ci的区别
From: http://www.chinaz.com/program/2010/0225/107151.shtml 当前,utf8_unicode_ci校对规则仅部分支持Unicode校对规则算法. ...
- u3d 场景资源打包
搞了一天终于搞定u3d的场景打包,这样就可以不用修改太多程序,把资源放在外部修改了.好处多多 但是本来很简单的东西搞了一天,google来的说作为场景scene.unity 文件 打包成 unityd ...
- Runtime、System、Object
Runtime---->Runtime.getRuntime().gc(); System---->System.gc(); Object---->finalize();
- java 坑
时间戳 unix的时间戳和java中的是不同的.unix为10位,java的13位.需要进行转换. long timestamps = 1509783992L; long javaTimstamps ...
- MySQL死锁分析
死锁问题背景 做MySQL代码的深入分析也有些年头了,再加上自己10年左右的数据库内核研发经验,自认为对于MySQL/InnoDB的加锁实现了如指掌,正因如此,前段时间,还专门写了一篇洋洋洒洒的文章, ...
- 12 go实现几中基本排序算法
include 冒泡排序 插入排序 快速排序 选择排序 这4种算法的内涵不再做解释了 github地址 冒泡排序算法 func maoPao(intSlice []int) []int { /* 冒泡 ...
- mysql出现1030 Got error 28 from storage engine解决方法
今天自己用 tp 写的项目报错 查了下,是磁盘临时空间不够导致 查看 my.cnf 的 tmpdir,看下指向哪个目录,修改到有空间的目录 最后发现是/var/tmp/phd/log/daemons. ...
- CorelDRAW中关于锁定与解锁对象的操作
在编辑复制的图形时,有时为了避免对象受到操作的影响,可以使用“锁定与解锁对象”功能键对已经编辑好的对象进行锁定.被锁定的对象将不能进行任何编辑操作,本教程将详解CorelDRAW中关于锁定与解锁对象的 ...
- OpenGL中的光照技术(翻译)
Lighting:https://www.evl.uic.edu/julian/cs488/2005-11-03/index.html 光照 OpenGL中的光照(Linghting)是很重要的,为什 ...
- pycharm使用docker容器的python解释器,
上一篇是pycharm调用docker的镜像的python解释器. 此篇介绍pycharm 调用docker的容器的python解释器. 这两个思路还是不一样的,第一个是用pycham界面的选择pyt ...