目录
一、概念
C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream.h头文件。
istringstream类用于执行C++风格的串流的输入操作。
ostringstream类用于执行C风格的串流的输出操作。
strstream类同时可以支持C风格的串流的输入输出操作。
istringstream类是从istream和stringstreambase派生而来,ostringstream是从ostream和 stringstreambase派生而来, stringstream则是从iostream类和stringstreambase派生而来。
他们的继承关系如下图所示:
istringstream是由一个string对象构造而来,istringstream类从一个string对象读取字符。
istringstream的构造函数原形如下:
istringstream::istringstream(string str);
C++ stringstream 类是一种十分有用的类,特别是当我们需要在程序中使用字符串和数字数据的时候。要想在程序中使用 stringstream 类,我们需要在源程序文件中包含头文件include<sstream>。stringstream 对象的使用方法与cout对象的使用方法基本相同。stringstream 类提供的函数,将数字化转化为字符串。
当我们需要按预定的格式将程序中的数据保存在一个string 中的时候,可以先创建一个stringstream 对象,并通过运算符 ”<<“ 将数据传递给 stringstream 对象。(这与通过”<<“ 使用cout 对象的方法相同。)接着,我们可以通过调用stringstream 类的函数str() 将对象所包含的内容赋给一个string对象。在一下的程序中,我们先将数据传递给一个stringstream 对象,然后通过该 stringstream 对象将数值赋给一个string 对象。住:cout能使用的所有ios格式标记也可以在stringstream 对象中使用。
二、基本功能
功能一:预定的格式将程序中的数据保存在一个string 中
// 如何使用 stringstream
// 对象生成格式化的 string
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
cout << "\n Welcome to the StringStream Demo program.\n";
// 构建一些将在string中出现的数据变量
// PI 精确到小数点后15位
double pi = 3.141592653589793;
float dollar = 1.00;
int dozen = 12;
string text;
// 我们希望tring 的格式如下:
// dozen适12,dollar是$1.00
// 精确到小数点后10为pi是3.141592653589793
// 生成stringstream 对象
stringstream ss;
// 现在像使用cout一样使用ss
ss << " A dozen is "<< dozen << ", a dollar is $ ";
ss.setf(ios::fixed);
ss.precision(2);
ss << dollar << " and \n the value of pi to 10 places is ";
ss.precision(10);
ss << pi << ".";
// 现在将ss中的内容赋给一个string对象
// 使用str()函数
text = ss.str();
cout << "\nHere is our formatted text string:\n" << text << endl;
// 再加入一些信息
ss << "\ There are 2 \"+\" in C++.";
text = ss.str();
cout<< "\nHere is the final string:\n" << text << endl;
return 0;
}
功能二:实现类型转换
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
double rb;
int ri; // 存储结果
string s; // 要转化的字符串
stringstream ss;
s = "123.456789";
ss << s; // 类似 cout
ss >> rb; // 类似 cin
cout.precision(10);
cout << "string \""<< s << "\" to double object "
<< rb << endl;
s = "654321";
ss.clear(); //清空流
ss << s;
ss >> ri;
cout << "string \""<< s << "\" to int object "
<< ri << endl;
return 0;
}
功能三:实现任意类型转换
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class t1="" class="" t2="">
int main()
{
double sb = 123.456;
int ri; // 存储结果
stringstream ss;
ss << sb;
ss >> ri;
cout << ri;
return 0;
}
该功能没多少实际意义,不如static_cast 来的简单。
三、重要功能
写这篇文章的目的主要是解决在笔试过程中,利用stringstream解决输入的问题。
解决问题的关键是,以stringstream为输入流,然后以getline识别分隔符然后进行处理。
常见输入是一行,然后将其放入vector中。
1.数字由空格或逗号分开。
5 1 2 3 4 5
#include<bits/stdc++.h>
//如果没有这个头文件
//看这里https://blog.csdn.net/qq_41687938/article/details/116535313
using namespace std;
int main() {
stringstream ss;
vector<int> nums;
string input;
getline(cin, input);
ss << input;
int num;
while (ss >> num) {
nums.push_back(num);
}
for (auto a : nums) {
cout << a << endl;
}
return 0;
}
因为stringstream默认输出由空格分开
2.数字由逗号分开
5,1,2,3,4,5
#include<bits/stdc++.h>
//如果没有这个头文件
//看这里https://blog.csdn.net/qq_41687938/article/details/116535313
using namespace std;
int main() {
stringstream ss;
vector<int> nums;
string input;
getline(cin, input);
ss << input;
string num;
while (getline(ss, num, ',')) {
nums.push_back(stoi(num));
}
for (auto a : nums) {
cout << a << endl;
}
return 0;
}
3.数字间不止一类符号分隔
5,[1,2,3,4,5]
如果除了分隔符还有其他无关字符,就得提取出来后,自己加判断了。
#include<bits/stdc++.h>
using namespace std;
int main() {
//std::string input;//{'5',',','[','1',',','2',',','3',',','4',',','5',']'};//5,[1,2,3,4,5]
getline(cin, input);
std::stringstream ss(input);
string str;
int a;
vector<int> nums;
while (getline(ss, str, ',')) {
if (!str.empty() && str[0] == '[') {
a = stoi(str.substr(1));
}
else if (!str.empty() && str[str.size() - 1] == ']')
a = stoi(str.substr(0, str.length() - 1));
else
a = stoi(str);
nums.push_back(a);
}
for (int i = 0; i < nums.size(); i++)
cout << nums[i] << ' ';
return 0;
}