sstream —— string流中读写数据

本文详细介绍了C++中sstream头文件定义的istringstream、ostringstream和stringstream三种类型,它们分别用于从string读取数据、向string写入数据以及同时进行读写操作。文章通过实例展示了如何使用这些类型进行数据处理,包括数据类型转换和多字符串流的集中输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sstream 头文件定义的三个类型来支持IO操作。

  • istringstream :从string中读取数据
  • ostringstream :向string中写入数据
  • stringstream:即可以从string 中读取数据,也可以向其中写入数据。

sstream的一些特有操作

// ssm01是一个未绑定的stringstream对象
stringstream ssm01;
// ssm02是一个stringstream对象,保存字符串 s 的一个拷贝
stringstream ssm02(s);

// 返回ssm02所保存的string字符串的拷贝
ssm02.str();
// 将字符串 s 拷贝到ssm01中,返回void
ssm01.str(s);

一、istringstream 读取string流

1、>> 方式读取

该方法将字符串中的数据读取,忽略所有空格。

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	string str01 = { " hello this @ just a test!  @ # && thanks " };
	istringstream ism(str01);
	string tmp;
	
	while (ism>>tmp)
	{
		cout << tmp << endl;
	}
	
	system("pause");
	return 0;
}

在这里插入图片描述

2、getline() 方式读取

该方式会将所有空格读取,其默认的分隔符是换行符号\n,可以自行设定分隔符
(1)、默认换行符为分割符

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	string str01 = { " hello this @ just a test!  @ # && thanks \nu some one is here" };
	istringstream ism(str01);
	string tmp;
	
	while (getline(ism,tmp))
	{
		cout << tmp << endl;
	}
	
	system("pause");
	return 0;
}

在这里插入图片描述
(2)、当自行设定分割符为@时,它会按照此字符来进行分割,但是当遇到换行符\n时,也会作为分割依据。

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	string str01 = { " hello this @ just a test!  @ # && thanks \nu some one is here" };
	istringstream ism(str01);
	string tmp;
	
	while (getline(ism,tmp,'@'))
	{
		cout << tmp << endl;
	}
	
	system("pause");
	return 0;
}

在这里插入图片描述


二、ostringstream 向string流写入数据

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	string str01 = "hello,,here,is";
	string str02 = "thanks for this! ";
	 // 创建ostringstream对象时绑定到指定字符串
	ostringstream osm01(str01);
	// 创建空对象,然后再输出到该对象
	ostringstream osm02;
	osm02 << str02;

	string res01 = osm01.str(); //将osm01中所保存的string字符串赋给str02
	string res02 = osm02.str();
	
	cout << "res01= " << res01 << endl;
	cout << "str02= " << res02 << endl;

	system("pause");
	return 0;
}

在这里插入图片描述


三、stringstream 向string中读写数据

1、用于不同数据类型间的转换
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	stringstream ssm;
	int num, num01 = 10;
	string str, str01 = "666";

	//将变量num1从int 类型转换成 string 类型
	ssm << num01; //向string流写入数据
	ssm >> str; // 向string流读取数据
	cout << "str= " << str << endl;

	ssm.clear();//清除流中的数据

	// 将变量str01从string 类型转换成int类型
	ssm << str01;
	ssm >> num;
	cout << "num= " << num << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

2、用于多string流集中输出
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	stringstream ssm;
	ssm << "hello " << "this is";
	ssm << " some";
	ssm << " @1234";

	cout << ssm.str() << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

### std::stringstream 的读写实现过程 #### 头文件引入 为了使用 `std::stringstream`,需要包含 `<sstream>` 头文件[^2]。 ```cpp #include <sstream> ``` #### 默认构造函数 创建一个默认的 `std::stringstream` 对象时,默认情况下该对象可以用于输入和输出操作。以下是默认构造的一个例子: ```cpp std::stringstream buf1; buf1 << 1; // 向中写入整数 1 int n = 0; buf1 >> n; // 从中读取数据到变量 n std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n'; ``` 上述代码展示了如何向 `std::stringstream` 写入数据以及从中读取数据[^3]。 #### 输入模式下的字符串 当需要仅作为输入使用时,可以选择 `std::istringstream` 类型的对象。以下是一个简单的示例: ```cpp std::istringstream inbuf("-1"); inbuf >> n; // 将 "-1" 转换为整数值并存储到变量 n 中 std::cout << "n = " << n << '\n'; ``` 此部分说明了如何利用 `std::istringstream` 进行只读操作。 #### 输出模式下的字符串 对于输出操作,则可选用 `std::ostringstream` 或者设置特定标志位来控制行为。例如,在追加模式下初始化 `std::ostringstream` 并执行写入动作如下所示: ```cpp std::ostringstream buf2("test", std::ios_base::ate); buf2 << '1'; // 在现有内容后面附加字符 '1' std::cout << buf2.str() << '\n'; ``` 这里体现了通过指定参数以改变初始状态从而影响后续逻辑的能力。 #### 关于模板特化 值得注意的是,`std::stringstream` 是基于更通用形式定义出来的具体版本之一——即它是 `std::basic_stringstream<char>` 的实例化结果[^4]。 综上所述,这些功能共同构成了 C++ 中灵活处理文本信息的基础工具集的一部分。 ```cpp // 综合示例程序展示完整的读写程 #include <iostream> #include <sstream> int main(){ // 初始化双向 std::stringstream ss; // 执行写入操作 ss << "The answer is "; double d = 42.0/9.0; ss << d; // 获取当前缓冲区中的全部内容 std::string result = ss.str(); // 清空内部指针位置以便重新解析已有的数据 ss.clear(); ss.seekg(0); char buffer[256]; ss.getline(buffer, sizeof(buffer)-1,' '); // 提取第一个单词"The" std::cout<<buffer<<"\n"; return 0; } ``` 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值