字符串流sstream[part3/使用字符串流进行安全的类型转换]

时间:2021-12-20 05:07:20

参考:

http://blog.163.com/zhuandi_h/blog/static/180270288201291710222975/

http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html

C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性、类型安全和可扩展性。
在本文中,我将展示怎样使用这些库来实现安全和自动的类型转换。
    如果你已习惯了<stdio.h>风格的转换,也许你首先会问:为什么要花额外的精力来学习基于<sstream>的类
型转换呢?也许对下面一个简单的例子的回顾能够说服你。假设你想用sprintf()函数将一个变量从int类型转
换到字符串类型。为了正确地完成这个任务,你必须确保证目标缓冲区有足够大空间以容纳转换完的字符
串。此外,还必须使用正确的格式化符。如果使用了不正确的格式化符,会导致非预知的后果。下面是一
个例子:
    int n = 1000;
    char s[10];
    sprintf(s, "%d", n);
    到目前为止看起来还不错。但是,对上面代码的一个微小的改变就会使程序崩溃:
    int n = 1000;
    char s[10];
    sprintf(s, "%f", n);
    在这种情况下,程序员错误地使用了%f格式化符来替代了%d。因此,s在调用完sprintf()后包含了一个不确
定的字符串。要是能自动推导出正确的类型,那不是更好吗?
    由于n和s的类型在编译期就确定了,所以编译器拥有足够的信息来判断需要哪些转换。<sstream>库中声明
的标准类就利用了这一点,自动选择所必需的转换。而且,转换结果保存在stringstream对象的内部缓冲中。
你不必担心缓冲区溢出,因为这些对象会根据需要自动分配存储空间。如:
    stringstream ss;
    string result = "1000";
    int n = 0;
    ss<<result;
    ss>>n;        //n等于1000

stringstream通常用来作类型转换

例子一:基本数据类型转string

/*基本数据类型转string*/
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
/*int转string*/
int n = 10;
string str;
stringstream stream; stream << n;
stream >> str;
cout<<str<<endl; //10 stream.clear(); //多次使用stringstream要先clear() /*char*转string*/
char cStr[10] = "China";
stream << cStr;
stream >> str;
cout<<str<<endl; //China system("pause");
return 0;
}

   例子二:string转基本数据类型  

/*string转基本数据类型*/
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
/*string转double*/
double n;
string str = "12.5";
stringstream stream; stream << str;
stream >> n;
cout<<n<<endl; //12.5 stream.clear(); //多次使用stringstream要先clear() /*string转char**/
string str1 = "China";
char cStr[10];
stream << str1;
stream >> cStr;
cout<<cStr<<endl; //输出China stream.clear(); //多次使用stringstream要先clear() int first, second;
stream<< "456";
stream >> first;
cout << first << endl; //456 stream.clear(); //多次使用stringstream要先clear()
stream << true;
stream >> second;
cout << second << endl; //1 system("pause");
return 0;
}

    在类型转换中使用模板

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

template<typename T>
void to_string(string& result, const T& t)
{
ostringstream oss;
oss<<t;
result = oss.str();
}

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

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类型:

#include <iostream>
#include <sstream>
using namespace std; template<typename out_type, typename in_value>
out_type convert(const in_value& t)
{
stringstream ss;
ss<<t;
out_type result;
ss>>result;
return result;
} int main()
{
double d;
string s = "12.56";
d = convert<double, string>(s);
cout<<d<<endl; //12.56
system("pause");
return 0;
}