Boost: lexical_cast is a conversion between values. For example, to convert a string "123" to an integer 123, the Code is as follows:
- string s = "123";
- 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
- 01 #include "stdafx.h"
- 02
- 03 #include <iostream>
- 04 #include <boost/lexical_cast.hpp>
- 05
- 06 using namespace std;
- 07 using namespace boost;
- 08
- 09 int main()
- 10 {
- 11 string s = "123";
- 12 int a = lexical_cast<int>(s);
- 13 double b = lexical_cast<double>(s);
- 14
- 15 printf("%d\r\n", a + 1);
- 16 printf("%lf\r\n", b + 1);
- 17
- 18 try
- 19 {
- 20 int c = lexical_cast<int>("wrong number");
- 21 }
- 22 catch(bad_lexical_cast & e)
- 23 {
- 24 printf("%s\r\n", e.what());
- 25 }
- 26
- 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