ca78a_c++_字符串流在内存中的输入输出(速度快)
/*ca78a_c++_字符串流在内存中的输入输出
**字符串流:在内存中的输入输出.(在内存中进行,速度快)
**文件流 :是对文件进行输入和输出.(在磁盘里面进行)
istringstream(输入),ostringstream(输出),stringstream(输入输出)
**字符串流stringstream特定的操作
stringstream strm;
stringstream strm(s);
strm.str()
strm.str(s)
**stringstream提供的转换和格式化,字符转字符,数字自动转数字
while (isstream >> word) //>>流输入操作符,一个一个的单词读取,空格作为单词分割的标志
welcome to discuss
txwtech@163.com
*/
/*ca78a_c++_字符串流在内存中的输入输出
**字符串流:在内存中的输入输出.(在内存中进行,速度快)
**文件流 :是对文件进行输入和输出.(在磁盘里面进行)
istringstream(输入),ostringstream(输出),stringstream(输入输出)
**字符串流stringstream特定的操作
stringstream strm;
stringstream strm(s);
strm.str()
strm.str(s)
**stringstream提供的转换和格式化,字符转字符,数字自动转数字
while (isstream >> word) //>>流输入操作符,一个一个的单词读取,空格作为单词分割的标志
welcome to discuss
txwtech@163.com
*/ #include <iostream>
#include <fstream>
#include <sstream>//stringstream(输入输出)头文件
#include <vector> using namespace std; int main()
{
cout << "hello" << endl; //文件输出流
ofstream ofs("test.txt");
ofs << "hello!" << endl;
ofs.close(); //字符串输出流
ostringstream oss;
oss << "字符串流hello!" << endl;//放在内存里面的
cout <<"字符串流里面的信息:"<< oss.str() << endl;//oss.str()查看流对象里面的字符串 //例子2:
string fileName, s;
vector<string> svec;
istringstream isstream;//输入字符串流
string word;
fileName = "book1.txt";
ifstream inFile(fileName.c_str());
if (!inFile)
{
cout << "文件打开错误:!!!" << __FILE__ << " " << __DATE__ << endl;
return -;
} while (getline(inFile, s))
svec.push_back(s);
inFile.close();
for (vector<string>::const_iterator iter = svec.begin(); iter != svec.end(); ++iter)
{
//cout << *iter << endl;
//把vector数据放入到输入字符串流里面
isstream.str(*iter);
while (isstream >> word)//>>流输入操作符,空格为间隔符号。一个一个单词的读取显示
{
//cout << word << endl;
}
isstream.clear();//字符串流清空,继续下一次循环
}
ostringstream format_message;//(字符串流)保存到内存,处理速度快
format_message << "姓名: " << "张飞" << "\n" << "age: " << << "\n" << "weight: " <<88.8 << "\n";
cout << "show ZhangFei:\n" << format_message.str() << endl; cout << "读取字符串流里面的数据" << endl;
string dump;
string name;
int age;
double weight;
istringstream input_istring(format_message.str());
input_istring >> dump; //"姓名:" ,format_message << "姓名: " 的姓名后面要有空格,才会读取准确
input_istring >> name;//"张飞"
input_istring >> dump;//"age: "
input_istring >> age;//
input_istring >> dump;//"weight: "
input_istring >> weight;//88.8
cout <<"name: "<< name <<" age:"<< age <<" weight: "<< weight << endl; return ;
}
ca78a_c++_字符串流在内存中的输入输出(速度快)的更多相关文章
- windows下查看C语言字符数组(俗称:字符串)在内存中地址信息的操作过程
#include <stdio.h> #pragma warning(disable:4996) int power10(int n) { ) { ; } ; ; i < n; ...
- C++学习50 对字符串流的读写
文件流是以外存文件为输入输出对象的数据流,字符串流不是以外存文件为输入输出的对象,而以内存中用户定义的字符数组(字符串)为输入输出的对象,即将数据输出到内存中的字符数组,或者从字符数组(字符串)将数据 ...
- 字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)?
字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)? 根据我的了解,编码中有三个核心概念:1. 字符集(Character Set),可以说是一个抽象概念,字符的合集2. ...
- C#中的流_字节_字符_字符串之间的相互转换
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- 在stream流和byte[]中查找(搜索)指定字符串
在 stream流 和 byte[] 中查找(搜索)指定字符串 这里注重看的是两个 Search 的扩展方法,一个是 stream 类型的扩展,另一个是 byte[] 类型的扩展, 如果大家有更好的“ ...
- Java中的字符串流的读取和写入(创建文件并判断重复账户)
各位我又来了!!哎!好心酸!我还没注册到三天!!没法登上博客的首页!!心累!! import java.io.BufferedOutputStream; import java.io.Buffered ...
- 字符串string和内存流MemoryStream及比特数组byte[]互转
原文:字符串string和内存流MemoryStream及比特数组byte[]互转 字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...
- 字符串在内存中的存储——C语言进阶
字符串是以ASCII字符NUL结尾的字符序列. ASCII字符NUL表示为\0.字符串通常存储在数组或者从堆上分配的内存中.只是,并不是全部的字符数组都是字符串,字符数组可能没有NUL字符. 字符数组 ...
- string字符串常量池在内存中的位置
这里仅仅是举个简单的样例说明字符串常量池在内存中的位置. 闲言少叙,直接上代码. <span style="font-size: large;">import java ...
随机推荐
- iOS开发MD5、SHA1
MD5: + (NSString *)md5:(NSString *)input { const char *cStr = [input UTF8String]; unsigned char dige ...
- 集合框架之ArrayList -Java
ArrayList 1.与数组的区别 如果要存放多个对象,可以使用数组,但是数组会有长度的限制,会出现不够用或者是浪费的情况. 为了解决数组的局限性引入了容器的概念,最常用的容器就是ArrayList ...
- Docker 入门:镜像
主要内容: 什么是镜像 下载镜像 pull 设置下载加速源 查看镜像 上传镜像 push 什么是镜像(image) 镜像是一个文件系统,提供了容器运行时需要用到的文件和参数配置.相当于平时在使用某个软 ...
- [PHP插件教程]002.代码包PHP Beautifier的使用
This program reformat and beautify PHP source code files automatically. The program is Open Source a ...
- gopher 协议初探
Gopher 协议初探 最近两天看到了字节脉搏实验室公众号上有一篇<Gopher协议与redis未授权访问>的文章,其中对gopher协议进行了比较详细的介绍,所以打算跟着后面复现学习一下 ...
- Ubuntu18.04兼容Python2.7、Python3.6、Python3.8以及pip、pip2、pip3问题
Ubuntu18.04兼容Python2.7.Python3.6.Python3.8以及pip.pip2.pip3问题 此为记录我重装Ubuntu后安装Python的过程 安装Python3.8 目前 ...
- Parrot os配置源更新
每次都是忘了怎么配置,去官网查文档,这记一下 一.源文件配置注意 首先要注意Parrot官方软件库的默认更新源文件不在 /etc/apt/sources.list 而是 /etc/apt/source ...
- [验证码识别技术] 字符型验证码终结者-CNN+BLSTM+CTC
验证码识别(少样本,高精度)项目地址:https://github.com/kerlomz/captcha_trainer 1. 前言 本项目适用于Python3.6,GPU>=NVIDIA G ...
- 使用turtle库画同切圆
import turtle as t t.setup(600,600,None,None) t.pensize(5) t.penup() t.pendown() t.pencolor("re ...
- Java实现 LeetCode 506 相对名次
506. 相对名次 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌&q ...