stringstream的基本用法

stringstream 是 C++ 标准库中的一个类,位于 头文件中。它是 C++ 中一个非常有用的工具,用于字符串的输入和输出流操作。stringstream 类提供了一些方法,可以方便地将字符串转换为其他数据类型,或者将数据类型转换为字符串。

stringstream 的主要用途:
从字符串中提取数据(类似于 cin 的功能)。
将数据转换为字符串(类似于 cout 的功能)。
stringstream 的基本用法:

1. 引入头文件

#include <sstream>

2. stringstream 的构造函数

stringstream 可以通过两种方式初始化:
默认构造函数:创建一个空的 stringstream。
使用字符串构造:从一个现有的字符串创建 stringstream 对象。

std::stringstream ss1;                    // 默认构造函数
std::stringstream ss2("123 456");          // 从字符串初始化

3. 基本方法

1. 输入操作(提取数据)

使用 >> 操作符从 stringstream 中提取数据,就像从 cin 中读取数据一样。

std::string str = "123 456 789";
std::stringstream ss(str);

int a, b, c;
ss >> a >> b >> c;  // 提取三个整数

std::cout << a << " " << b << " " << c << std::endl;  // 输出: 123 456 789

2. 输出操作(插入数据)

使用 << 操作符将数据插入到 stringstream 对象中,类似于 cout 的工作方式。

std::stringstream ss;
int a = 123;
float b = 45.67;

ss << a << " " << b;  // 向stringstream插入数据
std::cout << ss.str() << std::endl;  // 输出: "123 45.67"

3. str() 方法

str() 方法用于获取或设置 stringstream 中的字符串内容。

获取当前字符串内容:ss.str() 返回当前 stringstream 中的内容。
设置新的字符串内容:ss.str(new_string) 用来设置新的字符串。

std::stringstream ss;
ss << 100 << " " << 200;
std::cout << "Current content: " << ss.str() << std::endl;  // 输出: "100 200"

// 设置新的内容
ss.str("Hello, World!");
std::cout << "New content: " << ss.str() << std::endl;  // 输出: "Hello, World!"

4. clear() 方法

clear() 用于重置 stringstream 对象的状态标志,使其可以再次进行读写操作。

std::stringstream ss;
ss << 123;
std::cout << ss.str() << std::endl;  // 输出: 123

ss.clear();  // 清除状态标志
ss.str("");  // 清空内容
ss << "New string";
std::cout << ss.str() << std::endl;  // 输出: New string

5. eof() 和 fail() 方法

eof():返回一个布尔值,指示是否达到了流的末尾。
fail():返回一个布尔值,指示流是否出现错误。

std::stringstream ss("100 200");
int num;
while (ss >> num) {
    std::cout << num << " ";
}
if (ss.eof()) {
    std::cout << "\nEnd of input reached.\n";
}

6. seekg() 和 seekp() 方法

seekg():设置输入流的读取位置。
seekp():设置输出流的写入位置。

std::stringstream ss("123456");
ss.seekg(3);  // 设置输入流位置到第4个字符
char c;
ss >> c;
std::cout << c << std::endl;  // 输出: 4

7. tellg() 和 tellp() 方法

tellg():返回输入流的当前读取位置。
tellp():返回输出流的当前写入位置。

std::stringstream ss("abcdef");
ss.seekg(2);  // 设置输入流位置到第3个字符
std::cout << ss.tellg() << std::endl;  // 输出: 2

4. 从 stringstream 中读取多种数据类型

stringstream 能够处理多种数据类型的读取和写入,包括 int、float、double、char、string 等。

std::string str = "123 45.67 Hello";
std::stringstream ss(str);

int a;
float b;
std::string c;

ss >> a >> b >> c;  // 提取多个类型的数据

std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
// 输出: a = 123, b = 45.67, c = Hello

5. 处理格式化输出

stringstream 允许像 printf 一样进行格式化输出。你可以使用 std::fixed、std::setprecision 等来控制输出格式。

#include <iomanip>  // 引入格式化库

std::stringstream ss;
double pi = 3.14159;
ss << std::fixed << std::setprecision(2) << pi;  // 设置两位小数
std::cout << ss.str() << std::endl;  // 输出: 3.14

总结

输入输出:stringstream 可以像 cin 和 cout 一样进行输入输出操作。
格式化:支持数据格式化输出和控制字符串格式。
多种数据类型:支持从字符串中读取不同类型的数据,并将数据转换为字符串。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值