stringstream

本文详细介绍了C++中stringstream类的用法,包括用于输入、输出和混合操作。它提供了一种安全且类型安全的方式来进行数据转换,如将int转换为string,以及实现字符串拼接。通过示例代码展示了如何使用stringstream进行数据类型转换、多个字符串拼接以及清空stringstream的方法。此外,还提及了传统的sprintf和sscanf函数在数据处理中的作用。
摘要由CSDN通过智能技术生成

1 概述

定义了三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作。本文以 stringstream 为主,介绍流的输入和输出操作。

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

2 代码示例

2.1 数据类型转换
这里展示一个代码示例,该示例介绍了将 int 类型转换为 string 类型的过程。

#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;
}

2.2 多个字符串拼接
本示例介绍在 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;
}
  • 可以使用 str() 方法,将 stringstream 类型转换为 string 类型;

  • 可以将多个字符串放入 stringstream 中,实现字符串的拼接目的;

  • 如果想清空 stringstream,必须使用 sstream.str(""); 方式;clear()
    方法适用于进行多次数据类型转换的场景。

2.3 stringstream的清空
清空 stringstream 有两种方法:clear() 方法以及 str("") 方法,这两种方法有不同的使用场景。

#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;
}

sprintf函数

作用:

  • 将数字变量转换为字符串。

  • 得到整型变量的16进制和8进制字符串。

  • 连接多个字符串。

int main(){
    char str[256] = { 0 };
    int data = 1024;
    //将data转换为字符串
    sprintf(str,"%d",data);
    //获取data的十六进制
    sprintf(str,"0x%X",data);
    //获取data的八进制
    sprintf(str,"0%o",data);
    const char *s1 = "Hello";
    const char *s2 = "World";
    //连接字符串s1和s2
    sprintf(str,"%s %s",s1,s2);
    cout<<str<<endl; 
    return 0;
} 

sscanf函数

作用:

  • 根据格式从字符串中提取数据。如从字符串中取出整数、浮点数和字符串等。

  • 取指定长度的字符串

  • 取到指定字符为止的字符串

  • 取仅包含指定字符集的字符串

  • 取到指定字符集为止的字符串

int main(){
    char s[15] = "123.432,432";
    int n;
    double f1;
    int f2;
    sscanf(s, "%lf,%d%n", &f1, &f2, &n);
    cout<<f1<<" "<<f2<<" "<<n;
    return 0;
} 

输出结果:123.432 432 11, 即一共转换了11位的字符。\

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值