当前位置:   article > 正文

stringstream的使用

stringstream

写到290题使用stringstream简化步骤,学习一下使用

目录

小问题?

成员函数clear()

 那么问题来了?clear在啥时候用呢?

数据类型转换


<sstream>库定义了三种类:istringstream、ostringstream、stringstream

<sstream> 主要用来进行数据类型转换,比如可以将一个int类型的对象存入流中,然后经过流赋值给一个string的对象中,从而实现了int到string的转变,由于 <sstream> 使用 string 对象来代替字符数组,就避免缓冲区溢出的危险;而且,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。简单说,相比c库的数据类型转换而言,<sstream> 更加安全、自动和直接。
 

istringstream、ostringstream 一个负责流的输入,把字符串类型转为实际类型,就是把流里面的字符串输入到一个变量里面 ;  一个负责输出,把实际类型转为字符串类型,就是把数据输出到流里面

stringstream就是输入输出,二者结合

他们通过插入器"<<"和析取器">>"这两个运算符可以直接对流上数据进行操作。

istringstream搭配>>,ostringstream搭配<<

stringstream都能用,上面俩可以说是他的特化版本

使用 std::stringstream,你可以将字符串分割成单词、提取数字、格式化输出等。下面是一些 std::stringstream 的常见用法:

  1. 字符串输入:可以使用 << 运算符将数据插入到 std::stringstream 对象中,如下所示:

    1. std::stringstream ss;
    2. std::string str = "Hello, world!";
    3. int num = 42;
    4. ss << str << " " << num;
    5. cout << ss.str() << endl;
    6. // ss流里是Hello, world! 42
  2. ①.void str():无参数形式,用于将stringstream中的数据以string字符串的形式输出

    ②.void str(const string&s1):以字符串为参数,覆盖stringstream流中的数据

    1. ss.str("nihaoya");
    2. cout << ss.str() << endl;
  3. 字符串输出:可以使用 >> 运算符从 std::stringstream 对象中提取数据,如下所示:

    1. //ss流里是Hello, 42
    2. std::string tmp;
    3. int n;
    4. ss >> tmp >> n;
    5. cout<<tmp<<endl; //Hello,
    6. cout<<n<<endl; //42
  4. 分割字符串:可以使用 >> 运算符将 std::stringstream 对象中的字符串按空格分割成单词,如下所示:

    1. std::stringstream sp("Hello, world!");
    2. std::string word;
    3. while (sp >> word)
    4. {
    5. // 对每个单词进行处理
    6. cout << word << endl;
    7. }
    8. cout << word << endl;
  5. 格式化输出:可以使用 << 运算符将数据按指定格式输出到 std::stringstream 对象中,如下所示:

    1. std::stringstream ss;
    2. int num = 42;
    3. double pi = 3.14159;
    4. ss << "The number is: " << std::setw(5) << num << ", and PI is: " << std::setprecision(3) << pi;

小问题?

正常插入是这样,从末尾插入

  1. std::stringstream sp;
  2. std::string word("Hello, world!");
  3. sp<<word;
  4. cout<<sp.str()<<endl;
  5. string ty="sad44asd";
  6. sp<<ty;
  7. cout<<sp.str()<<endl;

 但是给流初始化之后再插入

从头插入的

  1. std::stringstream sp("Hello, world!");
  2. cout<<sp.str()<<endl;
  3. string ty="sad44asd";
  4. sp<<ty;
  5. cout<<sp.str()<<endl;

使用stringstream时的清空操作

在C++中可以使用stringstream来很方便的进行类型转换,字符串串接,不过注意重复使用同一个stringstream对象时要 先继续清空,而清空很容易想到是clear方法,而在stringstream中这个方法实际上是清空stringstream的状态(比如出错等),真正清空内容需要使用.str("")方法。
 

成员函数clear()

 利用成员函数clear()只能清空流的状态,但此时流占用的内存没有改变,多次调用同一个ss对象则导致内存会一直增加,因为stringstream不主动释放内存,若想改变内存,则需要配合成员函数void str(const string&s1)来完成。

为什么?看个例子:

  1. std::stringstream sp;
  2. std::string word("Hello, world!");
  3. sp<<word;
  4. cout<<sp.str()<<endl;
  5. string ty="sad44asd";
  6. sp<<ty;
  7. cout<<sp.str()<<endl;
  8. sp.clear();
  9. cout<<sp.str()<<endl;

 可以看到并没有清除内容,只有配合str("")才行,相当于把他覆盖成一个空字符串

  1. std::stringstream sp;
  2. std::string word("Hello, world!");
  3. sp<<word;
  4. cout<<sp.str()<<endl;
  5. string ty="sad44asd";
  6. sp<<ty;
  7. cout<<sp.str()<<endl;
  8. sp.str("");
  9. //sp.clear();
  10. cout<<sp.str()<<endl;

 那么问题来了?clear在啥时候用呢?

当要多次进行类型转换的时候用,啥意思 ,看示例代码

  1. int main()
  2. {
  3. stringstream sstream;
  4. int first, second;
  5. // 插入字符串
  6. sstream << "456";
  7. // 转换为int类型
  8. sstream >> first;
  9. cout << first << endl;
  10. // 在进行多次类型转换前,必须先运行clear()
  11. //sstream.clear();
  12. // 插入bool值
  13. sstream << true;
  14. // 转换为int类型
  15. sstream >> second;
  16. cout << second << endl;
  17. system("pause");
  18. }

理论上应该输出,456 和1 

 第二个转换输出了乱码,因为没有清除状态,可能导致之前的456仍然存在,影响到后面类型转换

加上clear,清除之前的状态,就能输出正确了

  1. stringstream sstream;
  2. int first, second;
  3. // 插入字符串
  4. sstream << "456";
  5. // 转换为int类型
  6. sstream >> first;
  7. cout << first << endl;
  8. // 在进行多次类型转换前,必须先运行clear()
  9. sstream.clear();
  10. // 插入bool值
  11. sstream << true;
  12. // 转换为int类型
  13. sstream >> second;
  14. cout << second << endl;
  15. system("pause");

数据类型转换

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

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

sting流:istringstream,ostringstream,stringstream。教会你str()和clear()的区别_istringstream.str_简单奥利奥的博客-CSDN博客

别的使用方法看这个 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/736004
推荐阅读
  

闽ICP备14008679号