转基本数据类型之间的任意转换
2009-02-15 23:43
471 查看
template<class out_type,class in_value>
out_type convert(const in_value &t)
{
stringstream stream;
stream.clear();//清空流
stream << t;//向流中传值
out_type result;//这里存储转换结果
stream >> result;//向result中写入值
return result;
}
利用stringstream流的输入输出可以将基本数据类型转化为其他类型。
整个可以利用下列程序进行测试,在这里没用加防错及纠错处理,敬请见谅。
#include <cstdlib>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
template<class out_type,class in_value> out_type convert(const in_value &t);
int main(){
int i = 100;
double d = 111.1;
float f = 1.0f;
string iStr = convert<string, int>(i);
string fStr = convert<string, float>(f);
string dStr = convert<string, double>(d);
cout << "After a convertion, " << endl;
cout << "the number is :" << iStr << endl;
cout << "the float is :" << fStr << endl;
cout << "the double is :" << dStr << endl;
cout << "After another convertion, " << endl;
int str2Int = convert<int, string>(iStr);
float str2f = convert<float, string>(fStr);
double str2d = convert<double, string>(dStr);
cout << "the number is :" << str2Int << endl;
cout << "the float is :" << str2f << endl;
cout << "the double is :" << str2d << endl;
return 0;
}
template<class out_type,class in_value>
out_type convert(const in_value &t)
{
stringstream stream;
stream.clear();//清空流
stream << t;//向流中传值
out_type result;//这里存储转换结果
stream >> result;//向result中写入值
return result;
}
out_type convert(const in_value &t)
{
stringstream stream;
stream.clear();//清空流
stream << t;//向流中传值
out_type result;//这里存储转换结果
stream >> result;//向result中写入值
return result;
}
利用stringstream流的输入输出可以将基本数据类型转化为其他类型。
整个可以利用下列程序进行测试,在这里没用加防错及纠错处理,敬请见谅。
#include <cstdlib>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
template<class out_type,class in_value> out_type convert(const in_value &t);
int main(){
int i = 100;
double d = 111.1;
float f = 1.0f;
string iStr = convert<string, int>(i);
string fStr = convert<string, float>(f);
string dStr = convert<string, double>(d);
cout << "After a convertion, " << endl;
cout << "the number is :" << iStr << endl;
cout << "the float is :" << fStr << endl;
cout << "the double is :" << dStr << endl;
cout << "After another convertion, " << endl;
int str2Int = convert<int, string>(iStr);
float str2f = convert<float, string>(fStr);
double str2d = convert<double, string>(dStr);
cout << "the number is :" << str2Int << endl;
cout << "the float is :" << str2f << endl;
cout << "the double is :" << str2d << endl;
return 0;
}
template<class out_type,class in_value>
out_type convert(const in_value &t)
{
stringstream stream;
stream.clear();//清空流
stream << t;//向流中传值
out_type result;//这里存储转换结果
stream >> result;//向result中写入值
return result;
}
相关文章推荐
- 基本数据类型和byte型之间的转换
- java 的基本数据类型之间的转换
- java基本数据类型之间的转换
- java基本数据类型之间的转换问题
- java中基本数据类型介绍及其之间的转换
- 基本数据类型、包装类、String之间的转换
- JAVA基本数据类型及之间的转换
- Java包装类(Wrapper)、基本数据类型、String类之间的转换
- java基本数据类型之间的转换
- OC 基本数据类型与对象之间的转换方法
- Java中的变量、基本数据类型及它们之间的转换
- java基础--->基本数据类型之间的转换
- OC基础:OC 基本数据类型与对象之间的转换方法 分类: ios学习 OC 2015-06-18 20:01 11人阅读 评论(0) 收藏
- Java的基本数据类型:8种基本类型、对应的包装类、引用数据类型及与String 之间的转换。
- 基本数据类型与字符串之间的转换
- 基本数据类型包装数据类型之间的转换
- 基本数据类型:8种基本类型、对应的包装类、引用数据类型及与String 之间的转换。
- 包装类来实现基本数据类型与字符串类型之间的转换:
- java 基本数据类型之间的转换
- C语言_基本数据类型范围、类型之间的转换规则、打印某个内存连续的内存值