实现任意类型的转换
template<typename out_type, typename in_value>
out_type convert(const in_value & t){
stringstream stream;
stream<<t;//向流中传值
out_type result;//这里存储转换结果
stream>>result;//向result中写入值
return result;
}
例子:
#include<iostream>
#include <sstream>
using namespace std;<pre name="code" class="cpp">int main(){
string test = "-123 9.87 welcome to, 989, test!";
istringstream iss;//istringstream提供读 string 的功能
iss.str(test);//将 string 类型的 test 复制给 iss,返回 void
string s;
cout << "按照空格读取字符串:" << endl;
while (iss >> s){
cout << s <<