当前位置: 首页 > news >正文

C++ - 标准库之 <sstream> ostringstream(ostringstream 概述、基本使用、清空内容、进阶使用)

一、std::ostringstream 概述

  1. std::ostringstream 是 C++ 标准库中的一个类,用于将数据格式化为字符串

  2. std::ostringstream 结合了流操作和字符串操作的功能,可以像使用 cout 一样向字符串写入数据


二、std::ostringstream 基本使用

  1. 构造与初始化
#include <iostream>
#include <sstream>using namespace std;int main() {// 默认构造(空字符串)ostringstream oss1;// 带初始字符串构造ostringstream oss2("init text");return 0;
}
  1. 写入数据与获取字符串
#include <iostream>
#include <sstream>using namespace std;int main() {ostringstream oss;oss << "Hello" << " " << 42 << " " << 3.14;string result = oss.str();cout << result << endl;// 清空内容oss.str("");return 0;
}
# 输出结果Hello 42 3.14

三、std::ostringstream 清空内容

1、如果不清空内容
  • 如果不清空内容,std::ostringstream 会保留之前写入的所有内容,后续写入会追加到已有内容之后
#include <iostream>
#include <sstream>using namespace std;int main() {ostringstream oss;oss << "Hello" << " World";string result1 = oss.str();cout << result1 << endl;string result2 = oss.str();cout << result2 << endl;return 0;
}
# 输出结果Hello World
Hello World
2、清空内容的方式
  1. 赋空字符串
oss.str("");
  1. 构造空字符串
oss.str(string());

四、std::ostringstream 进阶使用

1、格式化控制
#include <iostream>
#include <sstream>using namespace std;int main() {ostringstream oss;oss << hex << 255;string result = oss.str();oss.str("");cout << result << endl;return 0;
}
# 输出结果ff
#include <iostream>
#include <sstream>
#include <iomanip>using namespace std;int main() {ostringstream oss;oss << setw(10) << "text";string result = oss.str();oss.str("");cout << result << endl;return 0;
}
# 输出结果text
#include <iostream>
#include <sstream>
#include <iomanip>using namespace std;int main() {ostringstream oss;oss << setfill('*') << setw(8) << 42;string result = oss.str();oss.str("");cout << result << endl;return 0;
}
# 输出结果******42
#include <iostream>
#include <sstream>
#include <iomanip>using namespace std;int main() {ostringstream oss;oss << fixed << setprecision(2) << 3.14159;string result = oss.str();oss.str("");cout << result << endl;return 0;
}
# 输出结果3.14
2、追加内容
  1. str 方法用于完全替换流中的内容,且流的位置指针会被重置到开头,如果直接追加内容,追加内容会覆盖掉替换内容
#include <iostream>
#include <sstream>using namespace std;int main() {ostringstream oss;// 追加内容oss.str("New content");// 替换内容oss << " appended";string result = oss.str();oss.str("");cout << result << endl;return 0;
}
# 输出结果appendednt
  1. 使用 seekp 方法定位到行末,再追加内容
#include <iostream>
#include <sstream>using namespace std;int main() {ostringstream oss;// 追加内容oss.str("New content");// 定位到行末oss.seekp(0, ios::end);// 替换内容oss << " appended";string result = oss.str();oss.str("");cout << result << endl;return 0;
}
# 输出结果New content appended
3、流状态
  1. 操作成功
#include <iostream>
#include <sstream>using namespace std;int main() {ostringstream oss;// 成功写入操作oss << "Hello";if (oss.good()) {cout << "操作成功" << endl;}if (oss.fail()) {cout << "操作失败" << endl;}cout << "内容:" << oss.str() << endl;return 0;
}
# 输出结果操作成功
内容:Hello
  1. 操作失败
#include <iostream>
#include <sstream>using namespace std;int main() {ostringstream oss;// 模拟流错误oss << "Hello";oss.setstate(ios::failbit);oss << "Hello World";if (oss.good()) {cout << "操作成功" << endl;}if (oss.fail()) {cout << "操作失败" << endl;}cout << "内容:" << oss.str() << endl;return 0;
}
# 输出结果操作失败
内容:Hello

文章转载自:

http://k6IQvpVI.pznqt.cn
http://PgjQYlQ3.pznqt.cn
http://ysUGieyv.pznqt.cn
http://pBuUCJW9.pznqt.cn
http://OQiTEPzu.pznqt.cn
http://gxdyzoXp.pznqt.cn
http://oPC1S1eZ.pznqt.cn
http://eSgetf9j.pznqt.cn
http://UOwQ62Qo.pznqt.cn
http://1tzUO8KI.pznqt.cn
http://2r4wIMul.pznqt.cn
http://Ph1GqxrV.pznqt.cn
http://d58UO1Rp.pznqt.cn
http://4OJW3d9F.pznqt.cn
http://HoM7LNfY.pznqt.cn
http://X3iqrYn9.pznqt.cn
http://gFPHRS5q.pznqt.cn
http://8KHSsAds.pznqt.cn
http://ub6aFbRF.pznqt.cn
http://Tor8oJ8f.pznqt.cn
http://fm64kJkh.pznqt.cn
http://im8PIRzk.pznqt.cn
http://FEbqAuyK.pznqt.cn
http://HEgoohuN.pznqt.cn
http://NBu8GtCy.pznqt.cn
http://8tbQpKph.pznqt.cn
http://6YZwYd8X.pznqt.cn
http://jHLml6BC.pznqt.cn
http://9IBxw96f.pznqt.cn
http://MSELdMaA.pznqt.cn
http://www.dtcms.com/a/226964.html

相关文章:

  • Codeforces Round 1026 (Div. 2) C. Racing
  • 【笔记】为 Python 项目安装图像处理与科学计算依赖(MINGW64 环境)
  • 树欲静而风不止,子欲养而亲不待
  • 打开一个新的Maven工程要做的事情
  • 基于 StarRocks + Iceberg,TRM Labs 构建 PB 级数据分析平台实践
  • R语言基础| 创建数据集
  • langGraph多Agent
  • PH热榜 | 2025-06-02
  • Spring Boot中保存前端上传的图片
  • React 18 生命周期详解与并发模式下的变化
  • openai-java
  • 企业级开发中的 maven-mvnd 应用实践
  • 免费的硬盘工具
  • 电子电气架构 --- 后轮转向的一点事情
  • BUUCTF[ACTF2020 新生赛]Include 1题解
  • 《QDebug 2025年5月》
  • 2024年第十五届蓝桥杯Scratch10月stema选拔赛真题——数字卡片排序
  • [蓝桥杯]交换次数
  • [蓝桥杯]蚂蚁感冒
  • Spring Boot 3.X 下Redis缓存的尝试(一):初步尝试
  • BayesFlow:基于神经网络的摊销贝叶斯推断框架
  • php 各版本下载
  • NTP库详解
  • JavaScript性能优化:实战技巧提升10倍速度
  • 【笔记】如何卸载 MSYS2 中不同工具链的 numpy 包
  • TDengine 的 AI 应用实战——电力需求预测
  • vue-12 (路由守卫:全局、每个路由和组件内)
  • 黑马Java面试笔记之 微服务篇(SpringCloud)
  • 第12次12: 修改和删除收货地址
  • 前端面试宝典---前端水印