C++ ostringstream、istringstream
ostringstream:
1、关于tellp()函数
#include <iostream>
#include <sstream>
int main()
{std::ostringstream s;std::cout << s.tellp() << '\n';s << 'h';std::cout << s.tellp() << '\n';s << "ello, world ";std::cout << s.tellp() << '\n';s << 3.14 << '\n';std::cout << s.tellp() << '\n' << s.str();return 0;
}
运行结果:
参考文献:
https://zh.cppreference.com/w/cpp/io/basic_ostream/tellp
#include<iostream>
#include<sstream>
using namespace std;int main()
{ostringstream is;is.str("567");cout << is.str() << endl;is << "44";cout << is.str() <<endl;is << "@@@";cout << is.str() <<endl;return 0;
}
运行结果:
注意:第一次“56”被完全覆盖,第二次“7”也被覆盖。
原因(取自官网):
#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";
}
output:
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"
https://en.cppreference.com/w/cpp/io/basic_ostringstream/str
istringstream
1、
#include<iostream>
#include<sstream>
using namespace std;int main()
{istringstream is;is.str("567abc");int i = 0;is >>i;cout<<i<<endl;cout <<is.str() <<endl;return 0;
}
运行结果:
C++ ostringstream、istringstream相关推荐
- C++ : ostringstream、istringstream、stringstream
这篇博文写的很好,转载收藏,感谢原作者! C++引入了ostringstream.istringstream.stringstream这三个类,要使用他们创建对象就必须包含sstream.h头文件. ...
- C++ istringstream、ostringstream、strstream
C++三种流简介和用法 简介 C++引入了ostringstream.istringstream.strstream这三个类,要使用他们创建对象就必须包含这个头文件 istringstream类用于执 ...
- C++ istringstream、ostringstream、stringstream类用法详解
文章目录 0 概述 1 istringstream 2 ostringstream 3 stringstream 4 类型转换 5 reference 0 概述 C++引入了ostringstream ...
- istringstream、ostringstream、stringstream 类介绍
istringstream.ostringstream.stringstream 类介绍 c++的输入输出主要分为以下3种 标准流输入输出(基于控制I/O) 头文件:#include <iost ...
- C++中istringstream、ostringstream、stringstream详细介绍和使用
C++中istringstream.ostringstream.stringstream介绍和使用 (1)基于控制台的I/O 注意:提取符">>"从流中提取数据时跳过输 ...
- C++/C--istringstream、ostringstream、stringstream 类介绍【转载】
文章目录 1 C++的输入输出分为三种: 1.1 基于控制台的I/O 1.2 基于文件的I/O 1.3 基于字符串的I/O 2 头文件 3 功能 4 具体分析 4.1 istringstream类 4 ...
- 【c++基础】ifstream、istringstream的示例应用
本次示例所使用文件为phonebook.txt文件,该文件记录了一些人的名字以及电话,文件格式如下(每一行为一条记录,以人名开始,后面跟随一个或多个电话): james 15523951231 132 ...
- istringstream、ostringstream、stringstream 类介绍 .
0.C++的输入输出分为三种: (1)基于控制台的I/O (2)基于文件的I/O (3)基于字符串的I/O 1.头文件 [cpp] view plaincopyprint? #include < ...
- 用ostringstream与istringstream转化字符串
#include <sstream> //头文件是sstream string doubleToString(double inValue) const { ostringstream ...
最新文章
- java目录更改当前_Java-MVC:查看目录更改的最佳方法
- Field 'id' doesn't have a default value
- 马云牛啊 从骑自行车到坐迈巴赫只用20年
- MacOS AElf Boilerplate开发体验
- windows和linux多进程效率,Windows程序效率 (转)
- 蓝凌ekp开发_新华教育集团战略升级,携手蓝凌量身定制数字化办公平台
- java throw与throws_基于Java中throw和throws的区别(详解)
- python操纵excel的方法_python操作Excel的几种方式
- 前端学习(1321):node.js得异步api
- java多属性的map_java - 具有多个参数的MapStruct QualifiedByName - 堆栈内存溢出
- 【Scratch案例实操】Scratch字母排序 scratch编程案例教学 scratch创意编程 少儿编程教案
- Web——CSS页面背景
- 计算机桌面个性化怎样设置方法,电脑桌面图标怎么个性化自定义设置呢?
- 【干货】如何写微信公众号文章:文章流畅耐读的奥秘
- 在创业公司做运营,我总结了这些经验
- 请编写程序,读入CSV文件中数据,循环获得用户输入,直至用户直接输入“Q”退出。根据用户输入的星座名称,输出此星座的出生日期范围及对应字符形式。如果输入的名称有误,请输出“输入星座名称有误”
- Zhong__Centos安装Redis
- 【排序综合】直接插入排序,希尔排序,快速排序,堆排序,冒泡排序,简单选择排序的简介,实现和算法复杂度分析
- 《PFLD: A Practical Facial Landmark Detector》论文笔记
- 人类始终无法抗拒的十种心理