#include <strstream>
#include <sstream>
template <class T>
bool from_string(T &t, const std::string &s, std::ios_base & (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss>>f>>t).fail();
};
template<class T>
std::string to_string(const T &tvalue, std::ios_base & (*f)(std::ios_base&))
{
std::ostrstream oss;
oss << f << tvalue << '/0';
return oss.str();
};
#include <sstream>
#include <iostream>
void main()
{
int i;
float f;
// from_string()的第三个参数应为如下中的一个
// one of std::hex, std::dec 或 std::oct
if(from_string<int>(i, std::string("ff"), std::hex))
{
std::cout<<i<<std::endl;
}
else
{
std::cout<<"from_string failed"<<std::endl;
}
if(from_string<float>(f, std::string("123.456"), std::dec))
{
std::cout<<f<<std::endl;
}
else{
std::cout<<"from_string failed"<<std::endl;
}
}