#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
int main(void)
try{
//以下是内置类型向string转换的解决方案
//lexical_cast优势明显
int ival;
char cval;
ostringstream out_string;
string str0;
string str1;
ival = 100;
cval = 'w';
out_string << ival << " " << cval;
str0 = out_string.str();
str1 = lexical_cast<string>(ival)
+ lexical_cast<string>(cval);
cout << str0 << endl;
cout << str1 << endl;
//以下是string向内置类型转换的解决方案
//几乎和stringstrem相比,尚有不足,
//不知道各位有何想法>?
int itmpe;
char ctmpe;
str0 = "100k";
str1 = "100h";
istringstream in_string( str0 );
in_string >> itmpe >> ctmpe;
cout << itmpe << " " << ctmpe << endl;
itmpe = lexical_cast<int>(str1);
ctmpe = lexical_cast<char>(str1);
system( "PAUSE" );
return 0;
}
catch(bad_lexical_cast e)
{
cout << e.what() << endl;
cin.get();
}