zoukankan      html  css  js  c++  java
  • 【转载】C++中stringstream类常见用法

    本文主要介绍 C++ 中 stringstream 类的常见用法。

    1 概述

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

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

    2 代码示例

    2.1 数据类型转换

    这里展示一个代码示例,该示例介绍了将 int 类型转换为 string 类型的过程。示例代码(stringstream_test1.cpp)如下:

     1 #include <string>
     2 #include <sstream>
     3 #include <iostream>
     4 #include <stdio.h>
     5  
     6 using namespace std;
     7  
     8 int main()
     9 {
    10     stringstream sstream;
    11     string strResult;
    12     int nValue = 1000;
    13  
    14     // 将int类型的值放入输入流中
    15     sstream << nValue;
    16     // 从sstream中抽取前面插入的int类型的值,赋给string类型
    17     sstream >> strResult;
    18  
    19     cout << "[cout]strResult is: " << strResult << endl;
    20     printf("[printf]strResult is: %s
    ", strResult.c_str());
    21  
    22     return 0;
    23 }

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

    2.2 多个字符串拼接

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

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

     1 #include <string>
     2 #include <sstream>
     3 #include <iostream>
     4  
     5 using namespace std;
     6  
     7 int main()
     8 {
     9     stringstream sstream;
    10  
    11     // 将多个字符串放入 sstream 中
    12     sstream << "first" << " " << "string,";
    13     sstream << " second string";
    14     cout << "strResult is: " << sstream.str() << endl;
    15  
    16     // 清空 sstream
    17     sstream.str("");
    18     sstream << "third string";
    19     cout << "After clear, strResult is: " << sstream.str() << endl;
    20  
    21     return 0;
    22 }

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

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

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

    2.3 stringstream的清空

    清空 stringstream 有两种方法:clear() 方法以及 str("") 方法,这两种方法有不同的使用场景。str("") 方法的使用场景,在上面的示例中已经介绍了,这里介绍 clear() 方法的使用场景。示例代码(stringstream_test3.cpp)如下:

     1 #include <sstream>
     2 #include <iostream>
     3  
     4 using namespace std;
     5  
     6 int main()
     7 {
     8     stringstream sstream;
     9     int first, second;
    10  
    11     // 插入字符串
    12     sstream << "456";
    13     // 转换为int类型
    14     sstream >> first;
    15     cout << first << endl;
    16  
    17     // 在进行多次类型转换前,必须先运行clear()
    18     sstream.clear();
    19  
    20     // 插入bool值
    21     sstream << true;
    22     // 转换为int类型
    23     sstream >> second;
    24     cout << second << endl;
    25  
    26     return 0;
    27 }

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

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

    原文转载自https://blog.csdn.net/liitdar/article/details/82598039
  • 相关阅读:
    MongoDB入门
    MongoDB基础命令
    MongoDB查询
    MongoDB索引
    MongoDB聚合
    MongoDB进阶
    Elasticsearch简介与安装
    ElasticSearch索引
    shiro xml标准配置
    shiro双realm验证
  • 原文地址:https://www.cnblogs.com/PennyXia/p/12743255.html
Copyright © 2011-2022 走看看