fork download
  1. #include <iostream>
  2. #include <boost/lexical_cast.hpp>
  3. #include <string>
  4.  
  5. using namespace std;
  6. int main() {
  7. string str = "12345";
  8.  
  9. try{
  10. int num = boost::lexical_cast<int>(str);
  11. cout<< "Srting to num: " << num << endl;
  12.  
  13. double dbl = boost::lexical_cast<double>("12.34");
  14. cout<< "String to double: " << dbl << endl;
  15.  
  16. string strFromInt = boost::lexical_cast<string>(4567);
  17. cout<< "Integer to String: " << strFromInt<<endl;
  18. }
  19.  
  20. catch(const boost::bad_lexical_cast &e)
  21. {
  22. cerr<<"Error"<<e.what()<<endl;
  23. }
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
Srting to num: 12345
String to double: 12.34
Integer to String: 4567