赞
踩
一、介绍
<sstream> 定义了三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作, stringstream 主要用来进行数据类型转换。由于 stringstream 使用 string 对象来代替字符数组(snprintf方式),可以避免缓冲区溢出的危险;而且,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。
在C++中,stringstream 是一个流类,它允许你将字符串当做流进行读写操作。它是 <sstream> 头文件中定义的一部分,包含了 istringstream(用于输入),ostringstream(用于输出)和 stringstream(既可以用于输入也可以用于输出)。
stringstream 主要被用于以下几种情况:
- 字符串和其他数据类型之间的转换。
- 从字符串中逐个提取数据。
- 向字符串中逐个插入数据。
- 作为一般的字符串处理工具。
2.1 如果是空格,可以直接分割
#include <string> #include <sstream> #include <iostream> using namespace std; int main() { string str = "i am a boy"; stringstream is(str); string s; while (is >> s) { // 输出流 cout << s << endl; } system("pause"); return 0; }
使用getline()函数输出流
#include <iostream> #include <sstream> #include <string> #include <fstream> using namespace std; int main() { // string str = "i am a boy"; string sss = "1 2 3 4 5 6"; //有空格的字符串,其输出也是一行有空格的数:1 2 3 4 5 6 // string sss = "123456"; //此种无空格的字符串,输出也是无空格:123456 // stringstream is(str); stringstream is(sss); // string s; // while (is >> s) { // 输出流 // cout << s << endl; // } while (getline(is,s,',')) { // 输出流 //即使去掉字符‘,’,其输出也是带空格分割的 cout << s << endl; } system("pause"); return 0; }输出:
1 2 3 4 5 6
补充:
使用getline与stringstream分割字符串
1 getline
头文件:
getline()的原型是istream& getline ( istream &is , string &str , char delim );
其中 istream &is 表示一个输入流,
例如,可使用cin;
string str ; getline(cin ,str)
也可以使用 stringstream
stringstream ss(“test#”) ; getline(ss,str)
char delim表示遇到这个字符停止读入,通常系统默认该字符为’\n’,也可以自定义字符2 字符串分割
当我们直接利用getline(),自定义字符,从cin流中分割字符,例如
输入 “one#two”
string str; ss << str2; while (getline(cin, str, '#')) cout << str<< endl; system("pause"); return 0; 输出: one
#include <iostream> #include <sstream> #include <string> #include <fstream> using namespace std; int main() { string str; string str_cin("one#two#three"); stringstream ss; ss << str_cin; //将流ss中的字符串以分隔符#输出到字符串str中去 while (getline(ss, str, '#')) cout << str<< endl; system("pause"); return 0; } 输出: one two three
int main() { string str; string str_cin("one#two#three"); // string str_cin("one two three");//如果str_cin的初始值为one two three 以空格符的形式隔开,则最终输出则为一行:one two three stringstream ss; ss << str_cin; //将"one#two#three" 写入到流对象s中 while (getline(ss, str)) //去掉分割字符‘#’ cout << str<< endl; system("pause"); return 0; } 输出: one#two#three // string str_cin("one # two # three"); //用空格分开,则输出为:two 和three前分别有空格 one two three
2.2 如果输入的字符串要分别输出的话,必须使用 sstream.str("")来清空 stringstream
stringstream sstream; int first, second; // 插入字符串 sstream << "456"; // 转换为int类型 sstream >> first; cout << first << endl; // 清空 sstream sstream.str("") //等价于sstream.str(std::string()); sstream.clear(); // 清除eofbit标志位 // 插入bool值 sstream << true; // 转换为int类型 sstream >> second; // << 代表输入(写入), >> 代表输出(读取) cout << second << endl;输出:
stringstream 中存放多个字符串,实现多个字符串拼接
#include <string> #include <sstream> #include <iostream> using namespace std; int main() { stringstream sstream; // 将多个字符串放入 sstream 中 cout << "开始拼接" << endl; sstream << "first" << " " << "string,"; sstream << " second string"; cout << "最后的结果 is: " << sstream.str() << endl; cout << "清空 sstream" << endl; sstream.str(""); sstream << "third string"; cout << "After clear, 最后的结果 is: " << sstream.str() << endl return 0; }
逐个提取数据
你可以使用
stringstream
来逐个提取以特定格式存储在字符串中的数据,例如:
#include <sstream> #include <iostream> int main() { std::string data = "John 25 Programmer"; std::stringstream ss(data); std::string name; int age; std::string occupation; ss >> name >> age >> occupation; std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "Occupation: " << occupation << std::endl; }输出:
Name: John
Age: 25
Occupation: Programmer
这里,
stringstream
会根据空白字符自动分割字符串,并按照顺序将值赋给相应的变量。
使用
stringstream
进行类型转换以下是一个使用
stringstream
将数字转换为字符串的例子:
#include <sstream> #include <iostream> #include <string> int main() { int num = 123; std::stringstream ss; //此处将int数据插入到流中,不能使用stringstream ss(num),必须使用下面的方式ss << num ,否则插入为空,但是如果是string类型的可以这样使用。 ss << num; // 将整数放入流中 std::string str = ss.str(); // 使用str()函数 从流中提取字符串 std::cout << str << std::endl; // 输出:123 } 注意: 利用to_string将整数转化为string类字符串也可以 string s=to_string(int x); s.size();反过来,也可以将字符串转换为数值类型:
int main() { string line; while(getline(cin, line)) { int sum = 0, x; stringstream ss(line); // 将 line 复制到 stringstream ss 中 while(ss >> x) sum += x; // 相当于输入一个个的单词,自动将其转化换为数字 cout << sum << "\n"; } return 0; }
#include <sstream> #include <iostream> #include <string> int main() { std::string str = "456"; std::stringstream ss(str); // 初始化stringstream ,相当于将str复制到流对象中去 int num; ss >> num; // 从流中提取整数 std::cout << num << std::endl; // 输出:456 }
stringstream类是通过将字符串与流对象相关联来工作的。它可以将字符串内容读取到流对象中,也可以将流对象的内容转换为字符串。
stringstream是C++中非常有用的工具,可以方便地处理字符串的输入和输出。通过将字符串与流对象相关联,我们可以轻松地将字符串数据转换为其他类型(如整数或浮点数),或者将其他类型的数据转换为字符串。
2.1、创建对象
创建一个 stringstream 对象,可以使用默认构造函数。
std::stringstream ss;
2.2、写入数据
使用流操作符 << 将不同类型的数据写入 stringstream 中。可以连续写入多个数据。
int num = 42; std::string str = "Hello, World!"; float pi = 3.14159; ss << "The number is: " << num << "\n"; ss << "The string is: " << str << "\n"; ss << "The value of pi is: " << pi << "\n";
2.3、获取字符串
使用 str() 成员函数可以获取拼接后的字符串。
std::string result = ss.str();
2.4 字符串到流对象
要将字符串放入stringstream对象中,可以使用<<运算符。以下示例将字符串"Hello, World!"放入stringstream对象中:
std::stringstream ss; std::string str = "Hello, World!"; ss << str;现在,字符串"Hello, World!"已经被放入了stringstream对象ss中。
2.5、流对象到字符串
要从stringstream对象中提取内容并将其转换为字符串,可以使用>>运算符。以下示例从stringstream对象中提取内容并将其存储在字符串变量中:
std::stringstream ss; std::string str; ss >> str;现在,stringstream对象ss中的内容已被提取并存储在字符串变量str中。
声明:本文仅为学习stringstream类使用方法总结,欢迎批评指证,转载请注明出处!
参考:
1. C++ 中的 stringstream 的简单介绍以及使用_stringstream ss(valuestr);-CSDN博客
2. C++之stringstream类_c++ stringstream-CSDN博客
4. 【STL21】C++中的stringstream(字符串流)_c++ stringstream头文件-CSDN博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。