赞
踩
写到290题使用stringstream简化步骤,学习一下使用
目录
<sstream>库定义了三种类:istringstream、ostringstream、stringstream
<sstream> 主要用来进行数据类型转换,比如可以将一个int类型的对象存入流中,然后经过流赋值给一个string的对象中,从而实现了int到string的转变,由于 <sstream> 使用 string 对象来代替字符数组,就避免缓冲区溢出的危险;而且,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。简单说,相比c库的数据类型转换而言,<sstream> 更加安全、自动和直接。
istringstream、ostringstream 一个负责流的输入,把字符串类型转为实际类型,就是把流里面的字符串输入到一个变量里面 ; 一个负责输出,把实际类型转为字符串类型,就是把数据输出到流里面
stringstream就是输入输出,二者结合
他们通过插入器"<<"和析取器">>"这两个运算符可以直接对流上数据进行操作。
istringstream搭配>>,ostringstream搭配<<
stringstream都能用,上面俩可以说是他的特化版本
使用 std::stringstream
,你可以将字符串分割成单词、提取数字、格式化输出等。下面是一些 std::stringstream
的常见用法:
字符串输入:可以使用 <<
运算符将数据插入到 std::stringstream
对象中,如下所示:
- std::stringstream ss;
- std::string str = "Hello, world!";
- int num = 42;
- ss << str << " " << num;
- cout << ss.str() << endl;
- // ss流里是Hello, world! 42
①.void str():无参数形式,用于将stringstream中的数据以string字符串的形式输出
②.void str(const string&s1):以字符串为参数,覆盖stringstream流中的数据
- ss.str("nihaoya");
- cout << ss.str() << endl;
字符串输出:可以使用 >>
运算符从 std::stringstream
对象中提取数据,如下所示:
- //ss流里是Hello, 42
-
- std::string tmp;
- int n;
- ss >> tmp >> n;
-
- cout<<tmp<<endl; //Hello,
- cout<<n<<endl; //42
-
-
分割字符串:可以使用 >>
运算符将 std::stringstream
对象中的字符串按空格分割成单词,如下所示:
- std::stringstream sp("Hello, world!");
- std::string word;
- while (sp >> word)
- {
- // 对每个单词进行处理
- cout << word << endl;
- }
- cout << word << endl;
格式化输出:可以使用 <<
运算符将数据按指定格式输出到 std::stringstream
对象中,如下所示:
- std::stringstream ss;
- int num = 42;
- double pi = 3.14159;
- ss << "The number is: " << std::setw(5) << num << ", and PI is: " << std::setprecision(3) << pi;
正常插入是这样,从末尾插入
- std::stringstream sp;
- std::string word("Hello, world!");
-
- sp<<word;
- cout<<sp.str()<<endl;
-
- string ty="sad44asd";
- sp<<ty;
- cout<<sp.str()<<endl;
但是给流初始化之后再插入
从头插入的
-
- std::stringstream sp("Hello, world!");
-
- cout<<sp.str()<<endl;
-
- string ty="sad44asd";
- sp<<ty;
- cout<<sp.str()<<endl;
使用stringstream时的清空操作
在C++中可以使用stringstream来很方便的进行类型转换,字符串串接,不过注意重复使用同一个stringstream对象时要 先继续清空,而清空很容易想到是clear方法,而在stringstream中这个方法实际上是清空stringstream的状态(比如出错等),真正清空内容需要使用.str("")方法。
利用成员函数clear()只能清空流的状态,但此时流占用的内存没有改变,多次调用同一个ss对象则导致内存会一直增加,因为stringstream不主动释放内存,若想改变内存,则需要配合成员函数void str(const string&s1)来完成。
为什么?看个例子:
- std::stringstream sp;
- std::string word("Hello, world!");
-
- sp<<word;
- cout<<sp.str()<<endl;
-
- string ty="sad44asd";
- sp<<ty;
- cout<<sp.str()<<endl;
-
- sp.clear();
- cout<<sp.str()<<endl;
可以看到并没有清除内容,只有配合str("")才行,相当于把他覆盖成一个空字符串
-
- std::stringstream sp;
- std::string word("Hello, world!");
-
- sp<<word;
- cout<<sp.str()<<endl;
-
- string ty="sad44asd";
- sp<<ty;
- cout<<sp.str()<<endl;
-
- sp.str("");
- //sp.clear();
- cout<<sp.str()<<endl;
当要多次进行类型转换的时候用,啥意思 ,看示例代码
- int main()
- {
-
- stringstream sstream;
- int first, second;
-
- // 插入字符串
- sstream << "456";
- // 转换为int类型
- sstream >> first;
- cout << first << endl;
-
- // 在进行多次类型转换前,必须先运行clear()
- //sstream.clear();
-
- // 插入bool值
- sstream << true;
- // 转换为int类型
- sstream >> second;
- cout << second << endl;
-
- system("pause");
- }

理论上应该输出,456 和1
第二个转换输出了乱码,因为没有清除状态,可能导致之前的456仍然存在,影响到后面类型转换
加上clear,清除之前的状态,就能输出正确了
- stringstream sstream;
- int first, second;
-
- // 插入字符串
- sstream << "456";
- // 转换为int类型
- sstream >> first;
- cout << first << endl;
-
- // 在进行多次类型转换前,必须先运行clear()
- sstream.clear();
-
- // 插入bool值
- sstream << true;
- // 转换为int类型
- sstream >> second;
- cout << second << endl;
-
- system("pause");

这里展示一个代码示例,该示例介绍了将 int 类型转换为 string 类型的过程
- #include <string>
- #include <sstream>
- #include <iostream>
- #include <stdio.h>
-
- using namespace std;
-
- int main()
- {
- stringstream sstream;
- string strResult;
- int nValue = 1000;
-
- // 将int类型的值放入输入流中
- sstream << nValue;
- // 从sstream中抽取前面插入的int类型的值,赋给string类型
- sstream >> strResult;
-
- cout << "[cout]strResult is: " << strResult << endl;
- printf("[printf]strResult is: %s\n", strResult.c_str());
-
- return 0;
- }

别的使用方法看这个
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。