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 ...
随机推荐
- Salt-ssh批量自动安装被控端salt-mini
Salt-ssh是Saltstack的另外一种管理方式,无需安装minion端,可以运行salt的一切功能,管理和使用方法基本和salt一样.但是,salt-ssh并没有继承原来的ZeroMQ通讯架构 ...
- Mac OS终端中设置颜色高亮和自动补全
已测试通过,原文:http://blog.csdn.net/songjinshi/article/details/8945809 一.颜色高亮显示 针对terminal采用bash模式: 编辑 ~/. ...
- centos6.5 手动安装gcc
gcc版本:gcc-4.4.7 rpm -Uvh mpfr-2.4.1-6.el6.x86_64.rpm rpm -Uvh cpp-4.4.7-16.el6.x86_64.rpmrpm -Uvh ...
- 解决js中window.location.href不工作的问题
E6中在html中<a>标识中通过JS添加click事件调用一个JS函数,例如: < script type = "text/javascript" > ...
- python 使用循环生成list
#-*- coding:UTF-8 -*- squares=[] for x in range(10): squares.append(x**2) print squares #[0, 1, 4, 9 ...
- 常用的PostBuild Event 脚本
参考: http://blog.csdn.net/fengzhijiaxu/article/details/4827364 http://blog.csdn.net/teng_ontheway/art ...
- Openlayers 3计算长度和面积
1.比较粗糙的计算方式 计算长度 var length = lineFeature.getGeometry().getLength(); if (length > 1000) { length ...
- script 里写 html 模版
js模版引擎(例如:template.js 或 handlebars.js)一般都用<script>标签来存放模版的内容 1)模版写在<script>标签和写在<div& ...
- 【代码审计】CLTPHP_v5.5.3后台任意文件下载漏洞分析
0x00 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chich ...
- PHP代码审计笔记--CSRF漏洞
0x01 前言 CSRF(Cross-site request forgery)跨站请求伪造.攻击者盗用了你的身份,以你的名义向第三方网站发送恶意请求,对服务器来说这个请求是完全合法的,但是却完成了攻 ...