关于stringstream的一些总结


C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性、类型安全和可扩展性。可以使用这些库来实现安全和自动的类型转换。

如果你已习惯了<stdio.h>风格的转换,也许你首先会问:为什么要花额外的精力来学习基于<sstream>的类型转换呢?也许对下面一个简单的例子的回顾能够说服你。假设你想用sprintf()函数将一个变量从int类型转换到字符串类型。为了正确地完成这个任务,你必须确保证目标缓冲区有足够大空间以容纳转换完的字符串。此外,还必须使用正确的格式化符。如果使用了不正确的格式化符,会导致非预知的后果。下面是一个例子:

 

int n=10000;

char s[10];

sprintf(s,”%d”,n);// s中的内容为“10000”


到目前为止看起来还不错。但是,对上面代码的一个微小的改变就会使程序崩溃:
int n=10000;

char s[10];

sprintf(s,”%f”,n);// 看!错误的格式化符


在这种情况下,程序员错误地使用了%f格式化符来替代了%d。因此,s在调用完sprintf()后包含了一个不确定的字符串。要是能自动推导出正确的类型,那不是更好吗?

由于ns的类型在编译期就确定了,所以编译器拥有足够的信息来判断需要哪些转换。<sstream>库中声明的标准类就利用了这一点,自动选择所必需的转换。而且,转换结果保存在stringstream对象的内部缓冲中。你不必担心缓冲区溢出,因为这些对象会根据需要自动分配存储空间。

<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。简单起见,我主要以stringstream为中心,因为每个转换都要涉及到输入和输出操作。

注意,<sstream>使用string对象来代替字符数组。这样可以避免缓冲区溢出的危险。而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。
string到int的转换
std::stringstream stream;
string result=”10000”;
int n=0;
stream<<result;
stream>>n;//n等于10000

重复利用stringstream对象

如果你打算在多次转换中使用同一个stringstream对象,记住再每次转换前要使用clear()方法;到底是用clear()还是.str("")还没找到原因

在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。

在类型转换中使用模板

你可以轻松地定义函数模板来将一个任意的类型转换到特定的目标类型。例如,需要将各种数字值,如int、long、double等等转换成字符串,要使用以一个string类型和一个任意值t为参数的to_string()函数。to_string()函数将t转换为字符串并写入result中。使用str()成员函数来获取流内部缓冲的一份拷贝:

template<class T>

void to_string(string & result,const T& t)

{

 ostringstream oss;//创建一个流

oss<<t;//把值传递如流中

result=oss.str();//获取转换后的字符转并将其写入result
}

这样,你就可以轻松地将多种数值转换成字符串了:

to_string(s1,10.5);//double到string

to_string(s2,123);//int到string

to_string(s3,true);//bool到string

可以更进一步定义一个通用的转换模板,用于任意类型之间的转换。函数模板convert()含有两个模板参数out_type和in_value,功能是将in_value值转换成out_type类型:

template<class out_type,class in_value>

out_type convert(const in_value & t)

{

stringstream stream;

stream<<t;//向流中传值

out_type result;//这里存储转换结果

stream>>result;//向result中写入值

return result;

}

这样使用convert():

double d;

string salary;

string s=”12.56”;

d=convert<double>(s);//d等于12.56

salary=convert<string>(9000.0);//salary等于”9000”

结论

在过去留下来的程序代码和纯粹的C程序中,传统的<stdio.h>形式的转换伴随了我们很长的一段时间。但是,如文中所述,基于stringstream的转换拥有类型安全和不会溢出这样抢眼的特性,使我们有充足得理由抛弃<stdio.h>而使用<sstream>。<sstream>库还提供了另外一个特性—可扩展性。你可以通过重载来支持自定义类型间的转换。

 

一些实例:

stringstream通常是用来做数据转换的。

相比c库的转换,它更加安全,自动和直接。

基本数据类型转换例子 int转string

[cpp]  view plain copy
  1. #include <string>  
  2. #include <sstream>  
  3. #include <iostream>   
  4. #include <stdlib.h>    // system()  
  5.   
  6. int main(void)  
  7. {  
  8.     std::stringstream stream;  
  9.     std::string result;  
  10.     int i = 1000;  
  11.     stream << i; //将int输入流  
  12.     stream >> result; //从stream中抽取前面插入的int值  
  13.     std::cout << result << std::endl; // print the string "1000"  
  14.    system("PAUSE");   
  15.    return(0);  
  16. }   

 

除了基本类型的转换,也支持char *的转换。

[cpp]  view plain copy
  1. //功能:将int类型数据通过stringstream对象转成char[]   
  2. #include <iostream>   
  3. #include <stdlib.h>    // system()  
  4. #include <string>  
  5. #include <sstream>  
  6. using namespace std;  
  7. int main(void)  
  8. {  
  9.     std::stringstream stream;  
  10.     char result[12] = {'/0'};  
  11.     stream << 1234;                     //insert int to stream  
  12.     stream >> result;                   //extract previously inserted value  
  13.     cout << result << endl;             // print "1234"               
  14.       
  15.     system("PAUSE");   
  16.     return 0;  
  17. }  

再进行多次转换的时候,必须调用stringstream的成员函数clear().

[cpp]  view plain copy
  1. #include <sstream>  
  2. #include <iostream>  
  3. int main(void)  
  4. {  
  5.     std::stringstream stream;  
  6.     int first, second;  
  7.     stream<< "456"//插入字符串  
  8.     stream >> first; //转换成int  456  
  9.     std::cout << first << std::endl;  
  10.     stream.clear(); //在进行多次转换前,必须清除stream  
  11.     stream << true//插入bool值  
  12.     stream >> second; //提取出int 1  
  13.     std::cout << second << std::endl;  
  14.     return 0;  
  15. }   

可以将int,char*等不同类型的输入同时导入到一个stream对象,再通过该stream对象导出一个新值

[cpp]  view plain copy
  1. //功能:将int类型数据和char*数据通过stringstream对象转成char[]   
  2. #include <iostream>   
  3. #include <stdlib.h>    // system()  
  4. #include <string>  
  5. #include <sstream>  
  6. using namespace std;  
  7. int main(int argc, char *argv[])  
  8. {  
  9.     std::stringstream stream;  
  10.     char ip[16];  
  11.     stream << 218; //insert int  
  12.     stream << "."//insert string  
  13.     stream << 192; //insert int  
  14.     stream << "."//insert string  
  15.     stream << 160; //insert int  
  16.     stream << "."//insert string  
  17.     stream << 228; //insert int  
  18.       
  19.     stream >> ip;  
  20.     cout << "ip:/t" << ip << endl;    //print " 218.192.160.228"       
  21.     system("PAUSE");   
  22.     return 0;  
  23. }  

 

下面这个例子将字符串"mallid=310103123456/ntermno=139654785231/norder=00003995/ngcode=0000130/npaydate=20051031/nname=/ncardno=5248000262355365/nlimit=0804/nprice=2210"/n为分界符,提取出所有的字符串,可以用stringstream解决

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <sstream>  
  3. #include <vector>  
  4. #include <algorithm>  
  5. #include <ITERATOR>  
  6. #define INPUT_STRING "mallid=310103123456/ntermno=139654785231/norder=00003995/ngcode=0000130/npaydate=20051031/nname=/ncardno=5248000262355365/nlimit=0804/nprice=2210"     
  7. typedef std::vector<std::string> strvec;  
  8. int main(void)  
  9. {  
  10.     using namespace std;  
  11.     istringstream iss(INPUT_STRING);  
  12.     strvec strList;  
  13.     string strTemp;  
  14.     while(iss >> strTemp)  
  15.     {  
  16.         strList.push_back(strTemp);  
  17.     }        
  18.         // output the result  
  19.     copy(strList.begin(), strList.end(), ostream_iterator<string>(cout, "/n"));  
  20.     return 0;  
  21. }  

还可以这样写

[cpp]  view plain copy
  1. copy( istream_iterator<string>(istringstream(str)), istream_iterator<string>(), back_inserter(strList) );   
  2.   
  3. copy( strList.begin(), strList.end(), ostream_iterator<string>(cout,"/n") );   

补充备注:

对于清空stringstream缓存到底用stream.clear(),还是stream.str("") 的问题

clear只是重置流的状态标志,str清空缓存,vs08跟踪了下,clear后缓存还在,str确实将缓存清空

但clear可以正常工作,str后不能接着工作。。

在C++有两种字符串流,一种在sstream中定义,
另一种在strstream中定义。
它们实现的东西基本一样。
strstream里包含
class strstreambuf;
class istrstream;
class ostrstream;
class strstream;
它们是基于C类型字符串char*编写的
sstream中包含
class istringstream;
class ostringstream;
class stringbuf;
class stringstream;
class …….
它们是基于std::string编写的
因此ostrstream::str()返回的是char*类型的字符串
而ostringstream::str()返回的是std::string类型的字符串
在使用的时候要注意到二者的区别,一般情况下推荐使用std::string类型的字符串
当然如果为了保持和C的兼容,使用strstream也是不错的选择。
copy源码
[cpp]  view plain copy
  1. template<class InputIterator, class OutputIterator>  
  2.   OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )  
  3. {  
  4.   while (first!=last) *result++ = *first++;  
  5.   return result;  
  6. }  


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值