目录

一、前言

二、stringstream 是什么 ? 

 三、stringstream 的用法

 ✨构造函数

 ✨输出字符串

 ✨两种构造函数带来的不同

 ✨修改、清空 stringstream 内容

 四、stringsteam 的用途

✨ 利用 stringstream 去除字符串空格

✨ 利用 stringstream 指定字符分割字符串

✨ 类型转换

五、常考面试题

 ✨计算字符串中的单词个数:

 ✨ 反转字符串中的单词 (重点)

六、共勉  

一、前言

       想必大家在做 LeetCode 算法题的时候会经常看到有使用 stringstream 类 处理字符串,由于自己对这个类不是很了解,查了资料在这里记录一下。        所以本文通过收集资料将其进行总结,主要介绍如何使用 stringstream 处理在算法练习或竞赛中遇到的输入输出问题。

二、stringstream 是什么 ? 

 stringstream 是 C++  提供的专门用于处理字符串的 输入输出 流类。

这里稍微提一下c++中 “流” 的概念。在C++中,将数据从一个对象到另一个对象的流动抽象为“流”。流在使用前要被创建,使用后要被删除。数据的输入与输出是通过 I/O 流实现的,cin 和 cout 是c++预定义的流类对象。 

 stringstream 定义于头文件 ,它其实是个别名,具体定义如下:

typedef basic_stringstream stringstream;

类模板 std::basic_stringstream 实现基于字符串的流上的输入与输出操作。它等效地存储一个  std::basic_string 的实例,并在其上进行输入与输出操作。继承图如下: 

 三、stringstream 的用法

 ✨构造函数

 stringstream  的构造函数有很多,这里列举最为常用的两个构造函数:

1. 创建一个对象,向对象输入字符串:

// 创建一个 string类 对象 s

string s("hello stringstream");

// 创建一个 stringstraeam类 对象 ss

stringstream ss;

// 向对象输入字符串 : "<<" 表示向一个对象中输入

ss << s;

cout << ss.str() << endl;

2. 在创建对象的时候使用字符串初始化:

// 创建一个 stringstraeam类 对象 ss

stringstream ss("hello stringstream");

cout << ss.str() << endl;

 两种方式都可以创建对象,但创建后的对象用法不一样,详见后面的示例。

 ✨输出字符串

 stringstream  可以将存储于内部的字符串输出,需要调用 str() 函数,不可直接输出:

std::cout << ss.str() << std::endl;

// std::cout << ss << std::endl; // 错误不可直接输出

 注意:cout << ss << endl;   是错误的,不可以直接输出

✨两种构造函数带来的不同

 上面阐述了两种构造函数,利用不同的构造函数创建对象,对象具体的操作也不同:

1. 第一种构造方式

#include

#include

using namespace std;

int main()

{

stringstream ss1;

ss1 << "fre";

ss1 << "gre";

cout << ss1.str() << endl;

return 0;

}

/*

输出:

fregre

*/

可以发现,两个字符串直接拼接在了一起 

2. 第二种构造方式

#include

#include

using namespace std;

int main()

{

string str("asd");

// 第二种构造

stringstream ss2(str);

cout << ss2.str() << endl;

// 第一种构造

ss2 << "r";

cout << ss2.str() << endl;

ss2 << "13";

cout << ss2.str() << endl;

ss2 << "hy";

cout << ss2.str() << endl;

return 0;

}

/*

输出:

asd

rsd

r13

r13hy

*/

 可以发现,利用第一种构造函数创建对象时,输入字符串后直接进行字符串拼接,而第二种构造方式,在进行字符串拼接时,首先把原本的字符串覆盖掉,之后再进行拼接。

 如果不想原来的字符串被覆盖,则需要换一种构造方式,如下:

#include

#include

using namespace std;

int main()

{

ostringstream ss("1 2 3 4 ", std::ios_base::ate); // append 方式追加

cout << ss.str() << endl;

ss << "5 3 4";

cout << ss.str() << endl;

return 0;

}

/*

输出:

1 2 3 4

1 2 3 4 5 3 4

*/

  ✨修改、清空 stringstream 内容

 stringstream  的内容可以通过 str() 函数进行修改、清空:

#include

#include

using namespace std;

int main()

{

stringstream ss("hello string");

cout << ss.str() << endl;

// 修改内容

ss.str("hello stringstream");

cout << ss.str() << endl;

// 清空内容

ss.str("");

cout << ss.str() << endl;

return 0;

}

/*

输出:

fghewoo

123456

*/

 四、stringsteam 的用途

✨ 利用 stringstream 去除字符串空格

stringstream 默认是以空格来分割字符串的,利用 stringstream 去除字符串空格非常方便: 

#include

#include

using namespace std;

int main()

{

stringstream ss("hello string and stringstream");

cout << ss.str() << endl;

cout<< endl;

string str;

// 注意: stringstream 是一个单词一个单词 ”流入“ string 的

while (ss >> str)

{

cout << str << endl;

}

return 0;

}

/*

输出:

hello string and stringstream

hello

string

and

stringstream

*/

✨ 利用 stringstream 指定字符分割字符串

       上面描述了利用 stringstream 去除字符串空格,其实就是利用空格来分割字符串,同样,也可以指定其他字符对字符串进行分割,这需要与 getline() 函数搭配使用,下面以逗号分割字符串为例:

getline 详解:getline()的原型是istream& getline ( istream &is , string &str , char delim );

其中 istream &is 表示一个输入流,例如,可使用cin;string str ; getline(cin ,str)也可以使用 stringstreamstringstream ss(“test#”) ; getline(ss,str)char delim表示遇到这个字符停止读入,通常系统默认该字符为’\n’,也可以自定义字符

#include

#include

using namespace std;

int main()

{

string source = "abc,123,";

stringstream ss(source);

cout << ss.str() << endl;

cout<< endl;

string str;

while (getline(ss, str, ','))

{

cout << str << endl;

}

return 0;

}

/*

输出:

abc,123,

abc

123

*/

 上述代码以逗号作为分割依据来分割字符串,同样的还可以扩展到其他字符。

 ✨ 类型转换

使用 stringstream 进行类型转换

 以下是一个使用 stringstream 将数字转换为字符串的例子:

#include

#include

#include

int main() {

int num = 123;

std::stringstream ss;

ss << num; // 将整数放入流中

std::string str = ss.str(); // 使用str()函数 从流中提取字符串

std::cout << str << std::endl; // 输出:123

}

反过来,也可以将字符串转换为数值类型: 

#include

#include

#include

int main() {

std::string str = "456";

std::stringstream ss(str); // 初始化stringstream

int num;

ss >> num; // 从流中提取整数

std::cout << num << std::endl; // 输出:456

}

五、常考面试题

 ✨计算字符串中的单词个数:

输入:“hello world c plus plus” 输出:5 

#include

#include

#include

using namespace std;

int main() {

string str = "hello world c plus plus";

int count = 0;

stringstream ss(str);

string word;

while (ss >> word)

count++;

cout << count << endl;

system("pause");

return 0;

}

 ✨ 反转字符串中的单词 (重点)

链接:151. 反转字符串中的单词 

class Solution {

public:

string reverseWords(string s)

{

string res,temp;

stringstream ss(s);

while(ss>>temp)

{

res = temp + " " + res;

}

if(!res.empty())

{

res.pop_back();

}

return res;

}

};

六、共勉  

以下就是我对 stringstream类 的理解,如果有不懂和发现问题的小伙伴,请在评论区说出来哦,同时我还会继续更新对C++  vector 类的理解,请持续关注我哦!!!    

推荐文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。