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 ...
随机推荐
- C# 获取文件大小
当然了都需要引入System.IO这个命名空间 第一个: public static long GetDirectoryLength(string dirPath){//判断给定的路径是否存在,如果不 ...
- UNIX环境编程学习笔记(3)——文件I/O之内核 I/O 数据结构
lienhua342014-08-27 内核使用三种数据结构表示打开的文件,分别是文件描述符表.文件表和 V 节点表. (1) 每个进程在进程表中都有一个记录项,记录项中包含有一张打开文件描述符表,每 ...
- 性能分析Linux服务器CPU利用率
CPU度量 1. 指标范围 1.1 User mode CPU utilization+ System mode CPU utilization 合理值:60-85%,如果在一个多用户系统中us+ ...
- datetimerangepicker配置及默认时间段展示
<script type="text/javascript"> $(document).ready(function (){ //时间插件 $('#reportrang ...
- jekins job configure找不到remote trigger(script)
今天想测试一下,remote的方式启动一个job,但是在“构建触发器”一栏根本找不到remote trigger,很惊讶的是在网上所有的doc或者demo里都是有这个选项的. 最后,终于找到了原因: ...
- 用Fiddler查看 Android/iOS 网络请求
1.下载fiddler,尽量到官方网站找最新的版本 我这里也放了一个:http://files.cnblogs.com/xiaokang088/fiddler4setup.zip 2. 打开Fiddl ...
- abbyy在使用时提示更改分辨率如何处理
ABBYY FineReader 12识别质量取决于文档图像的分辨率.低图像分辨率(低于 150 dp)可能会降低识别质量,而过高的分辨率(高于 600 dpi)也不会明显提升图像质量,还会延长处理时 ...
- mysql中删除binlog的方法?mysql中如何删除binlog?
需求描述: 在mysql中如何删除binlog,因为随着数据库的运行,mysql中产生的binlog会越来越大,有可能把磁盘撑爆了,所以记录下删除 binlog的方法. 操作过程: 1.通过系统参数控 ...
- python 有class外壳不一定是oop,到底怎么oo?
1.继承 封装 多态是面向对象的三大特点,当年在考c++期末考试时候这三个特点还是作为填空题出现的. 而且老师也反复唠叨继承封装 多态,这三个词语是背得滚瓜烂熟了,但因为不是特别专业的计算机科班,学得 ...
- 一致性Hash算法原理及C#代码实现
一.一致性Hash算法原理 基本概念 一致性哈希将整个哈希值空间组织成一个虚拟的圆环,如假设某哈希函数H的值空间为0-2^32-1(即哈希值是一个32位无符号整形),整个哈希空间环如下: 整个空间按顺 ...