4.6 use third-party LibrariesThe preceding section describes how to assign values to, convert, and convert character codes to Visual C ++. In fact, there are some useful third-party class libraries to help C ++ programmers complete object processing, boost is a famous one. This section briefly introduces boost: any, boost: lexical_cast, and rational boost: rational in the boost library.4.6.1 universal boost: anyThe boost Library provides the any class. boost: any is a class that can save any type value. This is a bit like the variant type. However, because variant uses a huge union, low efficiency. While boost uses the template, it does not change the type of the value during saving, but only provides a method for the user to determine the type and value when necessary. Boost: any can be used to store almost any data type:
- boost::any ai, as;
- ai = 100;
- as = string("hello");
When necessary, we can use any_cast to restore the original data:
- int i = boost::any_cast<int>(ai);
- string s = boost::any_cast<string>(as);
When this type of conversion does not match, bad_any_cast will occur abnormally:
- try
- {
- int i = boost::any_cast<int>(as);
- }
- catch(boost::bad_any_cast & e)
- {
- }
In traditional C ++ programs, we have to use the omnipotent Pointer "void *" to support various data types, but unfortunately, the Conversion Based on universal pointers is insecure. The "void *" type check is missing. Therefore, we recommend that you use the any class whenever possible. Now you can write the following program to experience how to use boost: any to convert object types. Program 4-10: Use boost: any to convert Object Types
- 01 #include "stdafx.h"
- 02 #include "boost/any.hpp"
- 03 #include <string>
- 04
- 05 using namespace std;
- 06 using namespace boost;
- 07
- 08 class Cat
- 09 {
- 10 };
- 11
- 12 void print(any it)
- 13 {
- 14 if(it.empty())
- 15 {
- 16 printf("nothing!\r\n");
- 17 return;
- 18 }
- 19
- 20 if(it.type() == typeid(int))
- 21 {
- 22 printf("integer: %d\r\n", any_cast<int>(it));
- 23 return;
- 24 }
- 25
- 26 if(it.type() == typeid(string))
- 27 {
- 28 printf("string: %s\r\n", any_cast<string>(it).c_str());
- 29 return;
- 30 }
- 31
- 32 if(it.type() == typeid(CString))
- 33 {
- 34 _tprintf(_T("CString: %s\r\n"), any_cast<CString>(it));
- 35 return;
- 36 }
- 37
- 38 if(it.type() == typeid(Cat))
- 39 {
- 40 _tprintf(_T("oops! a cat!\r\n"));
- 41 return;
- 42 }
- 43 }
- 44
- 45 int main()
- 46 {
- 47 print(100);
- 48
- 49 any as[] = {any(), 100, string("hello"), CString("world"), Cat()};
- 50 for(int i = 0; i < sizeof(as) / sizeof(as[0]); i++)
- 51 {
- 52 print(as[i]);
- 53 }
- 54
- 55 return 0;56 }
The result output is 4-18.
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =".../attachment/200908/200908241251094430129 .jpg" border = "0"/>
========================================================== === 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 from section 4.6.1
This article is from the "Bai Qiao blog" blog, please be sure to keep this source http://bluejoe.blog.51cto.com/807902/192762