在C语言中,如果想要将一个整形变量的数据转化为字符串格式,可以使用下面的方法:
1、使用itoa()函数
2、使用sprintf()函数
但是两个函数在转化时,都得需要先给出保存结果的空间,那空间要给多大呢,就不太好界定,而且转化格式不匹配时,可能还会得到错误的结果甚至程序崩溃。
#include <iostream>
using namespace std;
int main()
{
int n = 123456789;
char s1[32];
itoa(n, s1, 10);//最后一个参数表示需要多少空间
char s2[32];
//sprintf就是相当于将打印到屏幕的字符串打印到用户给的空间
sprintf(s2, "%d", n);
char s3[32];
sprintf(s3, "%f", n);//将int转换成浮点数的时候会出错(把n这个空间看成浮点数)
return 0;
}
在C++中,我们可以使用stringstream类对象来避开此问题。在程序中如果想要使用stringstream,必须要包含头文件#include<sstream>
。在该头文件下,标准库包含三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作。
stringstream的只要用法如下:
1.将数值类型数据格式化为字符串
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#include<sstream>
int main()
{
int a = 12345678;
string sa;
// 将一个整形变量转化为字符串,存储到string类对象中
stringstream s;
s << a;
s >> sa;
// 将stringstream底层管理string对象设置成"",
// 否则多次转换时,会将结果全部累积在底层string对象中
s.str("");
s.clear();// 清空s, 不清空会转化失败
double d = 12.34;
s << d;
s >> sa;
string sValue;
//这个方法会把管理的整个string对象返回,不会受读/写指针的影响
sValue = s.str();// str()方法:返回stringsteam中管理的string类型
cout << sValue << endl;
return 0;
}
2.字符串拼接
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#include<sstream>
int main()
{
stringstream sstream;
// 将多个字符串放入 sstream 中
sstream << "first" << " " << "string,";
sstream << " second string";
cout << "strResult is: " << sstream.str() << endl;
// 清空 sstream
sstream.str("");
sstream << "third string";
cout << "After clear, strResult is: " << sstream.str() << endl;
return 0;
}
输出结果:
3. 用stringstream可以用指定字符分割字符串
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main() {
std::string data = "1_2_3_4_5_6";
std::stringstream ss(data);
std::string item;
queue<string> q;
cout << data << endl;
while (std::getline(ss, item, '_'))
cout << item << ' ';
}
输出结果:
总结:
- stringstream实际是在其底层维护了一个string类型的对象用来保存结果。
- 多次数据类型转化时,一定要用clear()来清空,才能正确转化,但clear()不会将
stringstream底层的string对象清空。 - 可以使用s. str("")方法将底层string对象设置为""空字符串。
- 可以使用s.str()将让stringstream返回其底层的string对象。
- stringstream使用string类对象代替字符数组,可以避免缓冲区溢出的危险,而且其会对参
数类型进行推演,不需要格式化控制,也不会出现格式化失败的风险,因此使用更方便,更
安全。
stringstream中的.clear()和.str()的理解:
需要重复使用同一个stringstream对象时,得先用.str(“”)方法清楚缓存,再用.clear()方法重设状态,否则对stringstream对象的操作是无效的。stringstream流就像是一个文件,而且就像是有自己的读写位置指针,对读写指针的理解如下:
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
int main()
{
string str;
while(getline(cin, str))
{
vector<int> v, w;
stringstream ss(str);(3)
int inter;
char ch;
while (ss >> inter)//(1)
{
v.push_back(inter);
ss >> ch;//读取逗号
}
ss.clear();
ss.str("");
getline(cin, str);
ss.clear();
ss << str;
cout << "str本身的值是:" << ss.str() << endl;
while(ss >> inter)//(2)
{
cout << inter;
}
}
return 0;
}
输出结果:
如果我们把ss.str(""); 这句注释掉,输出为:
我们还是注释掉ss.str(""),但是更改一下输入:
上面的结果说明ss流在(1)处while循环之后,其读指针(从流中读)已经在流开始的第2个位置处(从0位置开始数),而我们流的初始化时使用的stringstream ss(str)的方式,其写指针(写到流中)还是在流的开头,当我们写入两个数据时,即还并没有在读指针的位置写下有效数据,所以标号(2)的输出为空,当我们写入三个数据,即在读指针的位置处写下数字5,输出:5。
1、当使用ss.str("");时相当于将流变成了一个类似于新创建的干净的流,相当于把读指针和写指针都归0,字符串清空。
2、即使标号(1)处跳出了循环,此时再次使用ss.str(),发现会输出值:1,2,即,stringstream和cin不同,它并不清空流中的数据 ,只是在移动读写指针而已。
3、我们在使用流时,要注意读写指针的位置,一个比较好的习惯是,在创建流之后, 不进行标号(3)处的初始化,而是拆分为两条语句:
stringstream ss;
ss << str;