前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++编程语言中stringstream类介绍

C++编程语言中stringstream类介绍

作者头像
全栈程序员站长
发布2022-07-02 10:36:28
8390
发布2022-07-02 10:36:28
举报

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

本文主要介绍 C++ 编程语言中 stringstream 类的相关知识,同时通过示例代码介绍 stringstream 类的使用方法。

1 概述

<sstream> 定义了三个istringstreamostringstream stringstream,分别用来进行流的输入、输出和输入输出操作。本文以 stringstream 为主,介绍流的输入和输出操作。

<sstream> 主要用来进行数据类型转换,由于 <sstream> 使用 string 对象来代替字符数组(snprintf 方式),避免了缓冲区溢出的危险;而且,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符号的问题。简单说,相比 C 编程语言库的数据类型转换,<sstream> 更加安全、自动和直接。

2 示例代码

2.1 数据类型转换

这里展示一份示例代码,介绍将 int 类型转换为 string 类型的过程。

示例代码(stringstream_test1.cpp)的内容如下:

代码语言:javascript
复制
#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;
}

编译并执行上述代码,结果如下:

C++编程语言中stringstream类介绍
C++编程语言中stringstream类介绍

2.2 多个字符串拼接

本示例介绍在 stringstream 中存放多个字符串,实现多个字符串拼接的目的(其实完全可以使用 string 类实现),同时,介绍 stringstream 类的清空方法。

示例代码(stringstream_test2.cpp)的内容如下:

代码语言:javascript
复制
#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;
}

编译并执行上述代码,结果如下:

C++编程语言中stringstream类介绍
C++编程语言中stringstream类介绍

从上述代码执行结果能够知道:

  • 可以使用 str() 方法,将 stringstream 类型转换为 string 类型;
  • 可以将多个字符串放入 stringstream 中,实现字符串的拼接目的;
  • 如果想清空 stringstream,必须使用 sstream.str(“”); 方式;clear() 方法适用于进行多次数据类型转换的场景。详见示例 2.3。

2.3 stringstream的清空

清空 stringstream 有两种方法:clear() 方法以及 str(“”) 方法,这两种方法对应不同的使用场景。str(“”) 方法的使用场景,在上面的示例中已经介绍过了,这里介绍 clear() 方法的使用场景。

示例代码(stringstream_test3.cpp)的内容如下:

代码语言:javascript
复制
#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;
}

编译并执行上述代码,结果如下:

C++编程语言中stringstream类介绍
C++编程语言中stringstream类介绍

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

C++编程语言中stringstream类介绍
C++编程语言中stringstream类介绍

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 概述
  • 2 示例代码
    • 2.1 数据类型转换
      • 2.2 多个字符串拼接
        • 2.3 stringstream的清空
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档