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 ...
最新文章
- BFC与垂直外边距折叠笔记
- ubuntu vscode上使用cmake、编译、调试
- 13个代码注释的小贴士
- POJ - 3422 Kaka's Matrix Travels(网络流-最大费用最大流+拆点法)
- C# ---- GC中代的递增规律
- 剑指 Offer II 036. 后缀表达式
- 求解偏微分方程开源有限元软件deal.II学习--Step 9
- Javascript学习之创建对象
- 红米ac2100路由器刷入openwrt教程
- 网络百科——网络接口卡
- IE浏览器假死解决办法
- vim 修改注释颜色 tab键
- Tensorflow学习-自定义模型
- 如何高效优雅地下载CMIP6模式数据
- java货郎担问题求解_货郎担问题(TSP)
- 电脑应用图标变成白纸
- Pixie: A System for Recommending 3+ Billion Items to 200+ Million Users in Real-Time简介
- 计算机科学与技术一级学科硕士点,计算机科学与技术一级学科(硕士)、软件工程一级学科(硕士)和计算机技术、软件工程专业硕士学位授权点通过专家评审...
- STM32流水灯程序---2种方式
- iMagic,开启属于您的魔灯时刻