C++中stringstream多类型数据拼接和提取用法

1、std::stringstream 可以用于不同类型的拼接

在下面示例中,使用std::stringstream将整数、双精度浮点数和字符串连接到一个字符串中,这充分展示了其通用性。 << 运算符可以用于将各种数据类型追加到流中,而不仅仅是字符串。这使得 std::stringstream 成为一个强大的工具,可用于在C++中进行不同类型的拼接和字符串构建。

#include <iostream>
#include <sstream>
#include <string>
int main() 
{
    int intValue = 42;
    double doubleValue = 3.14159;
    std::string stringValue = "Hello, world!";
    
    std::stringstream ss;
    ss << "Int: " << intValue << ", Double: " << doubleValue << ", String: " << stringValue;
    
    std::string result = ss.str();
    std::cout << result << std::endl; 
    // 输出 "Int: 42, Double: 3.14159, String: Hello, world!"
    
    return 0;
}

2、std::stringstream 还可以用于从字符串中提取不同类型的数据到不同的变量中。

以下是一个示例

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string data = "42 3.14159 Hello";
    int intValue;
    double doubleValue;
    std::string stringValue;

    std::stringstream ss(data);	
    ss >> intValue >> doubleValue >> stringValue;

    std::cout << "Integer Value: " << intValue << std::endl;
    std::cout << "Double Value: " << doubleValue << std::endl;
    std::cout << "String Value: " << stringValue << std::endl;

    return 0;
}

>> 用于从流中提取数据并进行类型转换,默认分隔符为空格、制表符、换行符: \t\n。这允许你将不同类型的数据从字符串中解析并存储在不同的变量中。

如果想自定义分隔符,那就不能靠>>运算符实现提取了,需要借助std::getline()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++ stringstream 类,用于将字符串转换为各种数据类型,例如整数、浮点数和布尔值等,并支持将这些数据类型转换回字符串。 使用 stringstream 类需要包含头文件 <sstream>。以下是 stringstream 的一些基本用法: 1. 将字符串转换为整数: ```c++ #include <sstream> #include <string> #include <iostream> int main() { std::string str = "123"; int num = 0; std::stringstream ss(str); ss >> num; std::cout << num << std::endl; // 输出:123 return 0; } ``` 这里将字符串 "123" 转换为整数,并输出转换后的值 123。 2. 将整数转换为字符串: ```c++ #include <sstream> #include <string> #include <iostream> int main() { int num = 123; std::stringstream ss; ss << num; std::string str = ss.str(); std::cout << str << std::endl; // 输出:123 return 0; } ``` 这里将整数 123 转换为字符串,并输出转换后的字符串 "123"。 3. 将字符串转换为浮点数: ```c++ #include <sstream> #include <string> #include <iostream> int main() { std::string str = "123.45"; float num = 0; std::stringstream ss(str); ss >> num; std::cout << num << std::endl; // 输出:123.45 return 0; } ``` 这里将字符串 "123.45" 转换为浮点数,并输出转换后的值 123.45。 4. 将浮点数转换为字符串: ```c++ #include <sstream> #include <string> #include <iostream> int main() { float num = 123.45; std::stringstream ss; ss << num; std::string str = ss.str(); std::cout << str << std::endl; // 输出:123.45 return 0; } ``` 这里将浮点数 123.45 转换为字符串,并输出转换后的字符串 "123.45"。 除此之外,stringstream 还有许多其他的用法,例如支持多种数据类型的同时转换,或者支持定制化的格式化等,可以参考相关文档进行深入学习。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宗浩多捞

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值