You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
525 B

  1. #include <boost/lexical_cast.hpp>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. using boost::lexical_cast;
  7. int a = lexical_cast<int>("123");
  8. double b = lexical_cast<double>("123.0123456789");
  9. string s0 = lexical_cast<string>(a);
  10. string s1 = lexical_cast<string>(b);
  11. cout << "number: " << a << " " << b << endl;
  12. cout << "string: " << s0 << " " << s1 << endl;
  13. int c = 0;
  14. try {
  15. c = lexical_cast<int>("abcd");
  16. }
  17. catch (boost::bad_lexical_cast& e) {
  18. cout << e.what() << endl;
  19. }
  20. return 0;
  21. }