Universal converter boost: lexical_cast

Source: Internet
Author: User

Boost: lexical_cast is a conversion between values. For example, to convert a string "123" to an integer 123, the Code is as follows:

 
 
  1. string s = "123";  
  2. int a = lexical_cast<int>(s); 
This method is very simple. I strongly recommend that you forget many std functions and directly use boost: lexical_cast. If the conversion happens unexpectedly, lexical_cast will throw a bad_lexical_cast exception, so it needs to be captured in the program.

Now you can write the following program to experience how to use boost: lexical_cast to complete numerical conversion. Program 4-11: Use boost: lexical_cast to convert object values
 
 
  1. 01  #include "stdafx.h" 
  2. 02    
  3. 03  #include <iostream>  
  4. 04  #include <boost/lexical_cast.hpp>  
  5. 05    
  6. 06  using namespace std;  
  7. 07  using namespace boost;  
  8. 08    
  9. 09  int main()  
  10. 10  {  
  11. 11      string s = "123";  
  12. 12      int a = lexical_cast<int>(s);  
  13. 13      double b = lexical_cast<double>(s);  
  14. 14    
  15. 15      printf("%d\r\n", a + 1);  
  16. 16      printf("%lf\r\n", b + 1);  
  17. 17    
  18. 18      try 
  19. 19      {  
  20. 20          int c = lexical_cast<int>("wrong number");  
  21. 21      }  
  22. 22      catch(bad_lexical_cast & e)  
  23. 23      {  
  24. 24          printf("%s\r\n", e.what());  
  25. 25      }  
  26. 26    
  27. 27      return 0;28 } 
The above program implements the conversion from a string "123" to an integer and a double-precision real number. To prevent program cheating, we specifically asked it to add the value to 1), and the output result is 4-19.
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'class = fit-image height = "110" alt = "" src = "http://www.bkjia.com/uploads/allimg/131228/1T4341O0-0.jpg" width = "397" border = "0" big (this )? />
Click to view the big picture.) Figure 4-19 shows the running result.
This project corresponds to the "\ ch04 \ LexicalCastTest" directory in the CD ". ================================ 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://book.vcer.net/images/vcmap-s.gif "/> the above is taken from section 4.6.2 of the VC ++.

This article is from the "Bai Qiao blog" blog, please be sure to keep this source http://bluejoe.blog.51cto.com/807902/193411

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.