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

网站建设英文方案东北网站建设公司

网站建设英文方案,东北网站建设公司,wordpress 绑定手机版,iis7创建网站一、std::ostringstream 概述 std::ostringstream 是 C 标准库中的一个类,用于将数据格式化为字符串 std::ostringstream 结合了流操作和字符串操作的功能,可以像使用 cout 一样向字符串写入数据 二、std::ostringstream 基本使用 构造与初始化 #inc…

一、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://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://00000000.gjqgz.cn
http://www.dtcms.com/wzjs/620529.html

相关文章:

  • 静安网站开发网站建设功能覆盖范围
  • 怎么自己做直播网站嘉兴网站建设设计制作
  • 网站解析需要多长时间教育机构官网
  • 如何自己买域做网站电子商务网站建设与管理的理解
  • 上海网站制作平台中国网是什么级别的媒体
  • 北京正规网站建设公司哪家好网络营销效果好的网站
  • 黄页网站大全免费深圳模板建站平台
  • 东营北京网站建设杭州建设网站哪家好
  • 网站备份查询专门做运动鞋的网站
  • 怎么分辨网站是不是h5中国太空网站
  • 信阳做网站的公司设置网站字体
  • 阿里云可以建设多个网站wordpress jiathis 文章 去除
  • 大连html5网站建设费用二维码图片生成器在线制作
  • 手机网站建设教程网站建设开发模式h5
  • 培训机构网站模板大宗商品交易平台网
  • 网站用什么开发软件做网站开发一般黄了
  • 济南网站系统优化公司网站建设整体架构
  • 廊坊做网站优化的公司手游代理平台代理
  • 公司网站建设服务费怎么做账西安苗木行业网站建设价格
  • 网站建设市场背景dx365.wordpress
  • 商学院网站建设建议淘宝网站策划怎么做
  • 建设小微公司网站需要多少钱海报设计大赛
  • 宁陵县网站seo济南建设信息网站
  • 江苏网站建设工作室湖北网官网
  • 网站建设预计资金投入做视频特效的网站有哪些
  • wordpress能否做网站天水市秦州区作风建设年网站
  • 梅河口网站开发做网站需要哪些基本功能
  • 做网站公司宣传语东莞品牌网站建设多少钱
  • 祥云网站推广WordPress网站属于什么网站
  • wampserver做网站门户网站html下载