stringstream的用法

2023-05-16

1.用stringstream来分割指定的字符字符串,代码如下:

//用stringstream来分割指定的字符字符串
#include <iostream>
#include <sstream>
#include <vector>
#include<queue>
#include<string>

using namespace std;

int main() {
    string str = "1,2,3,4,5";
    stringstream ss(str);
    string item;
    queue<string> q;
    cout << str << endl;
    while(getline(ss, item, ','))
        cout << item << ' ';
    return 0;
}

运行结果:
在这里插入图片描述
2.字符串拼接,代码如下:

//stringstream用于字符串的拼接
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#include<sstream>
int main()
{
	stringstream sstream;
	// 将多个字符串放入 sstream 中
	sstream << "first" << " " << "second";
	sstream << " third times";
	cout << "输出字符串: " << sstream.str() << endl;
	// 清空 sstream
	sstream.str("");
	sstream << "one more time";
	cout << "清空字符串后,输出为: " << sstream.str() << endl;
	return 0;
}

运行结果:
在这里插入图片描述
3.将数值类型数据格式化为字符串,代码如下:

//stringstream将数值类型数据格式化为字符串
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#include<sstream>
int main()
{
	int a = 865;
	string sa;
	// 将一个整形变量转化为字符串,存储到string类对象中
	stringstream s;
	s << a;
	s >> sa;
	string strValue1;
	strValue1 = s.str();
	cout << strValue1 << endl;
	// 将stringstream底层管理string对象设置成"", 否则多次转换时,会将结果全部累积在底层string对象中
	s.str("");
	s.clear();// 清空s, 不清空会转化失败
	double d = 12.34;
	s << d;
	s >> sa;
	string strValue2;
	//这个方法会把管理的整个string对象返回,不会受读/写指针的影响
	strValue2 = s.str();// str()方法:返回stringsteam中管理的string类型
	cout << strValue2 << endl;
	return 0;
}

运行结果:
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

stringstream的用法 的相关文章

随机推荐

  • 用Photoshop进行icon的制作或将其它格式图片转成icon

    用Photoshop进行icon的制作或将其它格式图片转成icon 1 准备 1 安装的ps格式里没有ico xff0c 需要安装插件 ICOFormat 8bi xff0c 搜索 ICOFormat 8bi 并根据PC选择下载 xff1b
  • dataGridVie控件绑定List<T>数据

    1 实现功能 dataGridVie控件绑定List数据 xff0c 点击按钮更新List数据并重新绑定 xff0c dataGridVie控件的数据更新 2 编程步骤 xff08 1 xff09 定义Person类 span class
  • VS2019安装、卸载及升级程序打包过程

    1 安装打包工具 在VS2019界面点击扩展菜单下的管理扩展 xff0c 如果已安装Microsoft Visual Studio Installer Project xff0c 则如下图所示 如果没有安装则选择联机 xff0c 进行下载安
  • Modbus TCP协议

    1 Modbus协议 Modbus是一种工业总线协议标准 xff0c 包括ASCII RTU TCP三种报文类型 xff0c 其物理层接口有RS 232 RS 485 RS 422 及以太网 xff0c 采用主 从方式进行通信 2 Modb
  • C#文件拷贝的方法

    1 实现功能 xff1a 打开的文件夹如果和目标文件夹不一样 xff0c 则将文件拷贝到目标文件夹 span class token class name span class token keyword string span span
  • WPF控件样式设置

    1 直接在代码中设置 span class token operator lt span span class token class name Button span Content span class token operator 6
  • C#多线程日志的实现

    1 定义输出目标类型 span class token keyword public span span class token keyword enum span span class token class name LogTarget
  • The power input for PSU 2 is lost

    错误信息 Dell戴尔 PowerEdge R720 服务器错误 xff1a PSU0003 Power input for PSU 2 is lost Please check PSU cables 这个错误信息为 xff1a PSU 2
  • 如何保存token-localStorage存储

    1 原理 原理是通过vue router的beforeEach钩子 xff0c 在每次路由到一个地址的时候先判断该路由是否携带了meta信息 xff0c 且该信息中的requireAuth是否为true xff0c 如果为true表示该路由
  • 在Tomcat服务器部署jar包

    在Tomcat服务器部署jar包 1 输出jar包 在eclipse环境下 xff0c 进入Run Configuration界面 xff0c 在Maven Build下选择demo 将Goals的内容改成package 2 将jar包部署
  • PTA 程序设计天梯赛(1~20题)

    文章目录 1 Hello World 5 分 2 打印沙漏 20 分 3 个位数统计 15 分 4 计算摄氏温度 5 分 5 考试座位号 15 分 6 连续因子 20 分 7 A B 20 分 8 计算指数 5 分 9 计算阶乘和 10 分
  • 关于动态(长度不定)结构体数组的两种处理方法

    讲解这个问题 xff0c 以一个例子入手 xff1a PAT xff08 A xff09 1080 Graduate Admission 方法一 xff1a 定义一个结构体数组 xff0c 为该数组开辟一块大的存储空间 xff0c 然后进行
  • 关于C++中string头文件的用法

    注意 xff1a 这里需要声明一点 xff0c 头文件string和string h是不同的 下面的代码是string的使用例子 xff0c 在string头文件下 xff0c 函数中 xff0c 是可以采取string str 61 34
  • 基于VS2010下利用MFC编写软件控制安捷伦信号源

    程控信号源 最近接触关于写 自动化测试软件 xff0c 里面用到了 xff0c 需要实现频谱仪 信号源的程序控制 所以 xff0c 就把每天遇到的问题 xff0c 和学到的知识 xff0c 进行一个总结思考 信号源程控实现 xff1a 信号
  • Qt中emit的用法:发射信号

    emit是为了调用对应的槽函数 xff0c 用来发射信号
  • OAI:Ubuntu20.04不支持OAI

    遇到一个无法解决的问题 xff0c 将ubuntu从18 04升级到了20 04 xff0c 发现ubuntu20 04不支持OAI啊 xff01
  • OAI:eNB模块和UE模块的示波器显示

  • 力扣网页PC端无法进入(问题得到解决)

    最近发现在不同地方 xff0c 使用不同WiFi时 xff0c 有些地点出现力扣网页PC端无法加载的问题 按照网上方法进行了调试 xff0c 发现帖子推荐改hosts文件 xff0c 将自己电脑https leetcode cn com 的
  • Linux临时动态调整kvm虚拟机内存

    一 运维内容描述 同事反馈在用的虚拟机内存不足 xff0c 需要调整 查看一个虚拟机的内存情况 xff0c 最大是8G内存 xff0c 当前内存显示是4G 虚拟机调整最大内存是需要关闭虚拟机的 xff0c 但现在虚拟机上部了服务 xff0c
  • stringstream的用法

    1 用stringstream来分割指定的字符字符串 xff0c 代码如下 xff1a span class token comment 用stringstream来分割指定的字符字符串 span span class token macr