c++ sstream

c++ sstreamsstream定义了三个类:istringstream、ostringstream和stringstream分别用来进行流的输入、输出和输入输出操作由于sstream使用string对象代替字符数组,避免缓冲区溢出的危险;其次,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。相比c库的数据类型转换,sstream更加安全、自动和直接。1.数据类型转换#inclu…

大家好,又见面了,我是你们的朋友全栈君。

sstream定义了三个类:istringstream、ostringstream和stringstream分别用来进行流的输入、输出和输入输出操作
由于sstream使用string对象代替字符数组,避免缓冲区溢出的危险;其次,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。相比c库的数据类型转换,sstream更加安全、自动和直接。

1.数据类型转换

#include <string>
#include <sstream>
#include <iostream>
#include <stdio.h>
using namespace std;
 
int main()
{ 
   
    stringstream sstream;
    string strResult;
    int nValue = 1000;
 
    // 将int类型的值放入输入流中
    sstream << nValue;
    // 从sstream中抽取前面插入的int类型的值,赋给string类型
    sstream >> strResult;
 
    cout << "[cout]strResult is: " << strResult << endl;
    printf("[printf]strResult is: %s\n", strResult.c_str());
 
    return 0;
}

输出
[cout]strResult is: 1000
[printf]strResult is: 1000

2.字符串拼接

  • 注意清空sstream的方式 sstream.str(""),clear()适用于进行多次数据类型转换的场景
  • 可以使用str方法,将stringstream类型转换为string类型
  • 可以将多个字符串放入stringstream,实现字符串的拼接目的
#include <string>
#include <sstream>
#include <iostream>
 
using namespace std;
 
int main()
{ 
   
    stringstream sstream;
 
    // 将多个字符串放入 sstream 中
    sstream << "first" << " " << "string,";
    sstream << " second string";
    cout << "strResult is: " << sstream.str() << endl;
 
    // 清空 sstream
    sstream.str("");
    sstream << "third string";
    cout << "After clear, strResult is: " << sstream.str() << endl;
 
    return 0;
}

输出
strResult is: first string, second string
After clear, strResult is: third string

3.stringstream的清空

#include <sstream>
#include <iostream>
 
using namespace std;
 
int main()
{ 
   
    stringstream sstream;
    int first, second;
 
    // 插入字符串
    sstream << "456";
    // 转换为int类型
    sstream >> first;
    cout << first << endl;
 
    // 在进行多次类型转换前,必须先运行clear()
    sstream.clear();
 
    // 插入bool值
    sstream << true;
    // 转换为int类型
    sstream >> second;
    cout << second << endl;
 
    return 0;
}

输出
456
1

注意:在本示例涉及的场景下(多次数据类型转换),必须使用 clear() 方法清空 stringstream,不使用 clear() 方法或使用 str(“”) 方法,都不能得到数据类型转换的正确结果。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/135966.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • Pycharm Debug调试使用+代码调试理解

    Pycharm Debug调试使用+代码调试理解Pycharm使用教程03pycharm的Debug调试使用+代码调试理解。横着的七个按钮的功能:依次从左到右。ShowExecutionPoint:鼠标光标跳到程序运行到的位置StepOver:单步运行程序,不进入函数。在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数整个执行完再停止,也就是把子函数整个作为一步。在不存在子函数的情况下是和stepinto效果一样的。简单的说就是,程序代码越过子函数,但子函数会执行,且不进入。StepInto:在单步执行时,遇到子函数

    2022年10月31日
  • phpstorm2021最新激活码[在线序列号]

    phpstorm2021最新激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
  • springcloud启动错误[通俗易懂]

    springcloud启动错误[通俗易懂]标题:springcloud启动错误AfatalerrorhasbeendetectedbytheJavaRuntimeEnvironment:EXCEPTION_ACCESS_VIOLATION(0xc0000005)atpc=0x0000000000000000,pid=5900,tid=7172JREversion:Java™SERuntimeEnvironment(12.0.2+10)(build12.0.2+10)JavaVM:Jav

    2022年5月20日
  • Mac下利用Anaconda安装Opencv「建议收藏」

    Mac下利用Anaconda安装Opencv「建议收藏」打开Anaconda,选择Environments,打开需要安装环境的终端输入以下代码sudopipinstallopencv-python-ihttps://pypi.tuna.tsinghua.edu.cn/simple记住命令前加sudo,否则会报错填写密码后即可安装验证安装是否成功方法1importcv2没错报错就表明安装成功方法2condalist找到opencv库则表明安装成功!!Reference添加链接描述添加链接描述…

    2022年8月30日
  • 01_activiti7 学习笔记

    01_activiti7 学习笔记

    2021年7月12日
  • flyweight设计模式_适配器模式菜鸟

    flyweight设计模式_适配器模式菜鸟亨元模式动机模式定义实例结构要点总结笔记。动机在软件系统采用纯粹对象方案的问题在于大量细粒度的对象会很快充斥在系统中,从而带来很高的运行时代价—主要指内存需求方面的代价如何在避免大量细粒度对象问题的同时,让外部客户仍然能够透明地使用面向对象地方式来进行操作模式定义运用共享技术有效地支持大量细粒度地对象。实例每一个字符都是一个字体 字体对象Fontclass Font{private: //unique object key string key; //object

    2022年8月9日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号