1、<sstream> VS <stdio.h>:对于<stdio.h>风格的类型转换,学习基于<sstream>的类型转换是有必要的。<stdio.h>中,sprintf()函数将变量从int类型转换到string类型。首先,需要确保目标缓冲区有足够大空间以容纳转换完的字符串;其次,必须使用正确的格式化符,否则会导致非预知的后果。如:#include "stdafx.h"#include <iostream>#include <stdio.h>#include <sstream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ int i=10; char name[30]; sprintf(name,"pic\\left%02d.png",i); cout<<name<<endl; return 0;}如果使用了不正确的格式化符就会使结果错误:int i=10;char name[30];sprintf(name,”pic\\left%02f.png”,i);//%02fcout<<name<<endl;

2、<sstream><sstream>是C++新标准发行的,<sstream>库定义了三种类:istringstream流输入操作、ostringstream流输出操作和stringstream流输入输出操作。<sstream>优点:使用string对象代替char *,可以避免缓冲区溢出的危害;输入变量类型和输出对象类型被自动推导出来,即使使用了不正确的格式化符也没影响,更加安全;<sstream>流程:编译器首先在编译器确定输入和输出变量的类型,判读需要进行哪些变换;然后自动选择转换类型,转换结果保存在stringstream对象的内部缓存中,且不必担心缓冲区溢出,程序会自动为这些对象分配存储空间。例:string到int的类型转换:int _tmain(int argc, _TCHAR* argv[]){ string result="250"; int n=0; stringstream stream; stream << result; stream >> n;//n等̨¨于®¨²250 cout<<n<<endl; system("pause"); return 0;}

4、定义模板函数convert定义含有两个参数任意类型转换到特定目标类型的函数模板convert。例:将in_value值转换成out忧溲枷茫_type类型。template<class out_type,class in_value>out_type convert(const in_value & t){ stringstream stream; stream<<t;//向¨°流¢¡Â中D传ä?值¦Ì out_type result;//这a里¤?存ä?储ä¡é转Áa换?结¨¢果? stream>>result;//向¨°result中D写¡ä入¨?值¦Ì return result;}int _tmain(int argc, _TCHAR* argv[]){ double d; string salary; string ss="12.56"; d=convert<double>(ss);//string->doule salary=convert<string>(9000.0);//double->string cout<<d<<"\n"<<salary<<endl; system("pause"); return 0;}

