C++ code:string stream(string流)学习大全
问题一:
如果有一个文件aaa.txt,有若干行,不知道每行中含有几个整数,要编程输出每行的整数之和,该如何实现?由于cin>>不能辨别空格与回车的差异,因此只能用getline的方式逐行读入数据到string变量中,但在string变量中分离若干个整数还是稍显吃力。一个好的方法是用string流:
#include<iostream>
#include<sstream>
#include<fstream>
using namespace std;
int main()
{ifstream in("aaa.txt");for (string s; getline(in, s);){int a, sum = 0;for (istringstream sin(s); sin >> a; sum += a);cout << sum << endl;}cin.get();return 0;
}
讲道理,该程序编得有些放肆。本该将istringstream sin(s)单独占一行,结果非但不然,还将sum+=a都缩到循环结构描述的步长部分中去了。这样一来,循环体便为空了,于是,for循环的描述部分后面加上分号便自成独立的语句,但它确实能够完成累计工作。作为单独的循环,最后的“;”还是不能忘记的!!因为程序小,所以可读性还不到受伤害的地步,请读者来见识一下着这种风格。
istringstream是输入string流,它在sstream头文件中说明。该语句类似文件流操作,只不过创建sin流时,其参数为string对象。它是将string的实体看作是一个输入流,因而,sin>>a即是从string流中输入整数到a中,输啊输,一直输到string中的最后一个整数!
string流很有用,有时候要将内容逐个输出到string中,最后才根据计算结果来编排输出格式。这时候,用string流就很管用。
拓展:加入一个文件里面有一些实数,那么按照上述方法求和。
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{ifstream in("123.txt");string i_read;double d_number,sum = 0;while(getline(in,i_read)){for(istringstream s(i_read);s >> d_number;sum += d_number){cout << "double number is: " << d_number <<endl;cout << "sum is: " << sum << endl << endl;}}return 0;
}
以下是文件 123.txt 中的实数
1.23 2.21 3.123 1.2
1 2.2 .4 1.23
1 2 3
1.2 .1 .23
问题二:可以用于分割被空格、制表符等符号分割的字符串
#include<iostream>
#include<sstream> //istringstream 必须包含这个头文件
#include<string>
using namespace std;
int main(){ string str="i am a boy"; istringstream is(str); string s; while(is>>s) { cout<<s<<endl; }
}
输出结果:
i
am
a
boy
二、
//Example:stringstream、istringstream、ostringstream的构造函数和用法
#include <iostream>
#include <sstream>
int main()
{// default constructor (input/output stream)std::stringstream buf1;buf1 << 7;int n = 0;buf1 >> n;std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n';// input streamstd::istringstream inbuf("-10");inbuf >> n;std::cout << "n = " << n << '\n';// output stream in append mode (C++11)std::ostringstream buf2("test", std::ios_base::ate);buf2 << '1';std::cout << buf2.str() << '\n';
}
输出结果:buf1 = 7 n = 7
n = -10
test1
三、
//Example:stringstream的.str()方法
#include <sstream>
#include <iostream>
int main()
{int n;std::istringstream in; // could also use in("1 2")in.str("1 2");in >> n;std::cout << "after reading the first int from \"1 2\", the int is "<< n << ", str() = \"" << in.str() << "\"\n";std::ostringstream out("1 2");out << 3;std::cout << "after writing the int '3' to output stream \"1 2\""<< ", str() = \"" << out.str() << "\"\n";std::ostringstream ate("1 2", std::ios_base::ate);ate << 3;std::cout << "after writing the int '3' to append stream \"1 2\""<< ", str() = \"" << ate.str() << "\"\n";
}
输出结果:
after reading the first int from "1 2", the int is 1, str() = "1 2"
after writing the int '3' to output stream "1 2", str() = "3 2"
after writing the int '3' to append stream "1 2", str() = "1 23"
注意事项:
由于stringstream构造函数会特别消耗内存,似乎不打算主动释放内存(或许是为了提高效率),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消耗,因些这时候,需要适时地清除一下缓冲 (用 stream.str("") )。
参考链接:
istringstream、ostringstream、stringstream 类介绍 .
C++中的 istringstream 的用法
C++ code:string stream(string流)学习大全相关推荐
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) {MemoryStream ms = new ...
- 【Stream流学习】Java 8 新特性|Collectors.joining() 案例详解
[辰兮要努力]:hello你好我是辰兮,很高兴你能来阅读,昵称是希望自己能不断精进,向着优秀程序员前行! 博客来源于项目以及编程中遇到的问题总结,偶尔会有读书分享,我会陆续更新Java前端.后台.数据 ...
- Stream流学习记录
1. 创建流 单列集合: 集合对象.stream() List<Author> authors =new ArrayList<>();Stream<Author> ...
- Stream流练习题大全以及答案
练习一:过滤:filter.结果收集(数组) 有如下7个元素黄药师,冯蘅,郭靖,黄蓉,郭芙,郭襄,郭破虏,使用Stream将以郭字开头的元素存入新数组 public class Test {publi ...
- JavaSE学习总结(八)常用类(上)Object类==与equals方法的区别浅克隆的特点Scanner类String类String两种创建对象方式的区别String类的各种功能
JavaSE学习总结(八)常用类(上)/Object类/==与equals方法的区别/浅克隆的特点/Scanner类/String类/String两种创建对象方式的区别/String类的各种功能 常用 ...
- 【mysql新加不了中文】Error Code: 1366. Incorrect string value: ‘\xE7\xBA\xB8\xE7\xB1\xBB‘ for colum
INSERT INTO garbage.type ( id, type,type_sub) VALUES ( 1, "纸类","属于可回收物" ) Error ...
- java string类方法_Java基础学习——String类及其方法
String类概述 该类被final修饰,无子类,不可被复写.创建的对象一旦初始化,其内容不可被改变. String类复写了Object类中的equals()定义了自己的独特内容,该方法用于判断字符串 ...
- java中string类面试题_java中String类的面试题大全含答案
1.下面程序的运行结果是()(选择一项) String str1="hello"; String str2=new String("hello"); Syste ...
- String的底层分析 (学习笔记)
StringTable底层分析 String的基本特性 StringPool String的内存分配 字符串的拼接操作 拼接效率的对比 intern()的理解 new String("&qu ...
- 【两万字】IO流介绍大全,代码例题,内存图结构,一看就会
IO流对象 File类 文件 : File计算机中存储数据的 (音乐,电影,word,excel) 目录 : Directory 计算机中的文件夹,文件夹不能存储数据的,保存文件的容器 路径 : 文件 ...
最新文章
- 深度学习与PyTorch实战
- 链路分析 K.O “五大经典问题”
- 1线程概念:线程和进程之间的关系,线程间可共享资源,线程间非共享资源,线程的优缺点
- keycloak 调研资料
- 批量网站IP地址查询-BlueCatTools 最新版下载地址
- iOS 13-Sign In with Apple
- 51Nod-1087 1 10 100 1000【模拟】
- 数据结构笔记(五)-- 循环链表的实现
- 360浏览器升级_社畜必备!360浏览器上线“文档”功能 一键开启云办公
- Pycharm生成决策树
- BLE(12)—— 细说 Connection
- 网络安全技术第四章——身份认证技术(身份认证及方式、身份认证三要素、身份认证协议、KERBEROS协议、SSL协议)
- 现在流行的少儿编程是不是再收大家的智商税?来看看最中肯的回答
- ShardingSphere 5.2.1 发布|新增系统库、强制分片路由、一致性校验
- java nio MappedByteBuffer 文件映射
- 黑马程序员入学Java知识——精华总结
- powerquery分组,PowerQuery:如何连接分组值?
- TI公司Tina-ti和FilterProDesktop下载地址
- 高一下学期计算机,高一下学期自我评价
- 06.论Redis持久化的几种方式
热门文章
- 01.WPF中制作无边框窗体
- 用CURD来拯救前端的一切的一切
- 【WebGoat笔记】--- Cross-Site Scripting(XSS)
- 牛客网(剑指offer) 第十六题 合并两个排序的链表
- 303. 区域和检索 - 数组不可变
- 2014年第五届蓝桥杯C/C++ A组国赛 —— 第二题:六角幻方
- 2019第十届蓝桥杯C/C++ B组省赛 —— 第二题:年号字串
- 【STM32】DMA相关函数和类型
- 【Tools】MarkDown教程(一)-MarkDown简介
- 【Linux】一步一步学Linux——tar命令(62)