boost库的常用组件的使用(ZT)

简介:

1.boost::any

boost::any是一种通用的数据类型,可以将各种类型包装后统一放入容器内
最重要的它是类型安全的。有点象COM里面的variant.

使用方法:
any::type() 返回包装的类型
any_cast可用于any到其他类型的转化

 

None.gif #include  < boost / any.hpp > 
None.gif 
void  test_any()
ExpandedBlockStart.gif
 {
InBlock.gif typedef std::vector
 < boost::any >  many;
InBlock.gif many a;
InBlock.gif a.push_back(
 2 );
InBlock.gif a.push_back(
 string ( " test " ));
InBlock.gif
InBlock.gif 
 for (unsigned  int  i = 0 ;i < a.size(); ++ i)
ExpandedSubBlockStart.gif 
 {
InBlock.gif  cout
 << a[i].type().name() << endl;
InBlock.gif  
 try 
ExpandedSubBlockStart.gif   
 {
InBlock.gif   
 int  result  =  any_cast < int > (a[i]);
InBlock.gif   cout
 << result << endl;
ExpandedSubBlockEnd.gif  }
 

InBlock.gif  
 catch (boost::bad_any_cast  &  ex)
ExpandedSubBlockStart.gif  
 {
InBlock.gif   cout
 << " cast error: " << ex.what() << endl;
ExpandedSubBlockEnd.gif  }
 

ExpandedSubBlockEnd.gif }
 

ExpandedBlockEnd.gif}
 

None.gif 
None.gif

2.boost::array

boost::array仅仅是对数组一层薄薄的封装,提供跟各种算法配合的iterator,使用方法很简单
注意:可以使用{}来初始化array,因为array所有的成员变量都是public的

 

None.gif #include  < boost / array.hpp > 
None.gif 
void  test_array()
ExpandedBlockStart.gif
 {
ExpandedSubBlockStart.gif array
 < int , 10 >  ai  =   { 1 , 2 , 3 } ;
InBlock.gif
InBlock.gif 
 for (size_t i = 0 ;i < ai.size(); ++ i)
ExpandedSubBlockStart.gif 
 {
InBlock.gif  cout
 << ai[i] << endl;
ExpandedSubBlockEnd.gif }
 

ExpandedBlockEnd.gif}
 

None.gif 
None.gif

3.boost::lexical_cast
lexical_cast用于将字符串转换成各种数字类型(int,float,short etc.)

 

None.gif #include  < boost / lexical_cast.hpp > 
None.gif 
void  test_lexical_cast()
ExpandedBlockStart.gif
 {
InBlock.gif 
 int  i  =  boost::lexical_cast < int > ( " 123 " );
InBlock.gif cout 
 <<  i  <<  endl;
ExpandedBlockEnd.gif}
 

None.gif 
None.gif

4.boost::format 
boost::format是用于替代c里面的sprintf,优点是类型安全,不会因为类型和参数不匹配而导致程序崩溃了
而且还可以重复使用参数

 

None.gif #include  < boost / format.hpp > 
None.gif 
void  test_format()
ExpandedBlockStart.gif
 {
InBlock.gif cout 
 <<  boost::format( " writing %1%,  x=%2% : %3%-th try "  %   " toto "   %   40.23   %   50   << endl; 
InBlock.gif
InBlock.gif format f(
 " a=%1%,b=%2%,c=%3%,a=%1% " );
InBlock.gif f 
 %   " string "   %   2   %   10.0 ;
InBlock.gif
InBlock.gif cout 
 <<  f.str()  <<  endl;
ExpandedBlockEnd.gif}
 

None.gif 
None.gif

5.boost::tokenizer
boost::tokenizer是用于切割字符串的,类似于Java里面的StringTokenizer。

 

None.gif #include  < boost / tokenizer.hpp > 
None.gif  void  test_tokenizer()
ExpandedBlockStart.gif  {
InBlock.gif  string  s( " This is  , a ,test! " );
InBlock.gif boost::tokenizer <>  tok(s);
ExpandedSubBlockStart.gif  for (tokenizer <> ::iterator beg = tok.begin(); beg != tok.end(); ++ beg) {
InBlock.gif       cout  <<   * beg  <<   " \n " ;
ExpandedSubBlockEnd.gif } 

ExpandedBlockEnd.gif

None.gif 
None.gif


6.boost::thread 
boost::thread是为了提供跨平台的thread机制。利用boost::function来完成委托。

 

None.gif #include  < boost / thread.hpp > 
None.gif 
void  mythread()
ExpandedBlockStart.gif
 {
InBlock.gif cout
 << " hello,thread! " << endl;
ExpandedBlockEnd.gif}
 

None.gif 
None.gif 
void  test_thread()
ExpandedBlockStart.gif
 {
InBlock.gif boost::function
 <   void  ()  >  f(mythread);
InBlock.gif boost::thread t(f);
InBlock.gif t.join();
InBlock.gif cout
 << " thread is over! " << endl;
ExpandedBlockEnd.gif}
 

None.gif 
None.gif

7.boost::serialization
boost::serialization提供object的序列化功能。而且提供好几种序列化的格式,比如text,binary,xml

 

None.gif #include  < boost / archive / text_oarchive.hpp > 
None.gif#include  < boost / archive / text_iarchive.hpp > 
None.gif#include  < boost / archive / xml_oarchive.hpp > 
None.gif  void  test_serialization()
ExpandedBlockStart.gif  {
InBlock.gif boost::archive::text_oarchive to(cout , boost::archive::no_header);
InBlock.gif  int  i  =   10 ;
InBlock.gif  string  s  =   " This is a test\n " ;
InBlock.gif to  &  i;
InBlock.gif to  &  s;
InBlock.gif
InBlock.gif ofstream f( " test.xml " );
InBlock.gif boost::archive::xml_oarchive xo(f);
InBlock.gif xo  &  BOOST_SERIALIZATION_NVP(i)  &  BOOST_SERIALIZATION_NVP(s);
InBlock.gif
InBlock.gif boost::archive::text_iarchive ti(cin , boost::archive::no_header);
InBlock.gif ti  &  i  &  s;
InBlock.gif cout  << " i= " <<  i  <<  endl;
InBlock.gif cout  << " s= " <<  s  <<  endl;
ExpandedBlockEnd.gif

None.gif

 

8.boost::function 
boost::function就是所谓的泛函数,能够对普通函数指针,成员函数指针,functor进行委托,达到迟调用的效果

 

None.gif #include  < boost / function.hpp > 
None.gif 
int  foo( int  x, int  y)
ExpandedBlockStart.gif
 {
InBlock.gif cout
 <<   " (foo invoking)x =  " <<  <<   "  y =  " <<  y  << endl;
InBlock.gif 
 return  x + y;
ExpandedBlockEnd.gif}
 

None.gif 
None.gif 
struct  test
ExpandedBlockStart.gif
 {
InBlock.gif 
 int  foo( int  x, int  y)
ExpandedSubBlockStart.gif 
 {
InBlock.gif  cout
 <<   " (test::foo invoking)x =  " <<  <<   "  y =  " <<  y  << endl;
InBlock.gif  
 return  x + y;
ExpandedSubBlockEnd.gif }
 

ExpandedBlockEnd.gif}
 
;
None.gif
None.gif
 void  test_function()
ExpandedBlockStart.gif
 {
InBlock.gif boost::function
 < int  ( int , int ) >  f;
InBlock.gif f 
 =  foo;
InBlock.gif cout 
 <<   " f(2,3)= " << f( 2 , 3 ) << endl;
InBlock.gif
InBlock.gif test x;
ExpandedSubBlockStart.gif 
 /* f = std::bind1st(
ExpandedSubBlockEnd.gif      std::mem_fun(&test::foo), &x);
 */ 

InBlock.gif boost::function
 < int  (test * , int , int ) >  f2;
InBlock.gif f2 
 =   & test::foo;
InBlock.gif  
InBlock.gif cout 
 <<   " f2(5,3)= " << f2( & x, 5 , 3 ) << endl;
ExpandedBlockEnd.gif}
 

None.gif 
None.gif

9.boost::shared_ptr 
boost::shared_ptr就是智能指针的实现,不象std::auto_ptr,它是可以stl的容器一起使用的,非常的方便

None.gif
None.gif #include  < boost / shared_ptr.hpp > 
None.gif  class  Shared
ExpandedBlockStart.gif  {
InBlock.gif public :
InBlock.gif Shared()
ExpandedSubBlockStart.gif  {
InBlock.gif  cout  <<   " ctor() called " << endl;
ExpandedSubBlockEnd.gif } 

InBlock.gif Shared( const  Shared  &  other)
ExpandedSubBlockStart.gif  {
InBlock.gif  cout  <<   " copy ctor() called " << endl;
ExpandedSubBlockEnd.gif } 

InBlock.gif  ~ Shared()
ExpandedSubBlockStart.gif  {
InBlock.gif  cout  <<   " dtor() called " << endl;
ExpandedSubBlockEnd.gif } 

InBlock.gif Shared  &   operator   =  ( const  Shared  &  other)
ExpandedSubBlockStart.gif  {
InBlock.gif  cout  <<   " operator =  called " << endl;
ExpandedSubBlockEnd.gif } 

ExpandedBlockEnd.gif
;
None.gif
None.gif  void  test_shared_ptr()
ExpandedBlockStart.gif  {
InBlock.gif typedef boost::shared_ptr < Shared >  SharedSP;
InBlock.gif typedef vector < SharedSP >  VShared;
InBlock.gif VShared v;
InBlock.gif v.push_back(SharedSP( new  Shared()));
InBlock.gif v.push_back(SharedSP( new  Shared()));
ExpandedBlockEnd.gif
 
目录
相关文章
|
4月前
|
XML 存储 JSON
CocosCreator 面试题(十五)Cocos Creator如何内置protobuf JS版本?
CocosCreator 面试题(十五)Cocos Creator如何内置protobuf JS版本?
|
安全 C++ Windows
好工具推荐系列:VC++开发必备神器 -- Dependencies,查看依赖库DLL,支持win10,比depends更好用
好工具推荐系列:VC++开发必备神器 -- Dependencies,查看依赖库DLL,支持win10,比depends更好用
2122 0
好工具推荐系列:VC++开发必备神器 -- Dependencies,查看依赖库DLL,支持win10,比depends更好用
|
4月前
|
缓存 前端开发 JavaScript
【源码共读】Vue2源码 shared 模块中的36个实用工具函数分析
【源码共读】Vue2源码 shared 模块中的36个实用工具函数分析
79 0
|
Linux 开发工具 C语言
hi3559 C/C++混编 makefile(基于官方sample)
由于个人需要想做海思的C++和C混编,好像不认真学一学makefile不行了 本人博客,csdn搬运 main函数是卸载cpp里面的,写在c里面的就是给你们提供一个思路了 弄明白了有空自己写呐,海思的makefile感觉嵌套的太冗余了,正常开发一个片子用不到呐
188 0
|
存储 安全 Java
Qt三方库开发技术:log4Qt介绍、编译和使用
Qt三方库开发技术:log4Qt介绍、编译和使用
Qt三方库开发技术:log4Qt介绍、编译和使用
|
JavaScript 区块链 C++
好工具推荐系列:Total Commander,可以实现VS/Qt工程源码的一键备份
好工具推荐系列:Total Commander,可以实现VS/Qt工程源码的一键备份
269 0
好工具推荐系列:Total Commander,可以实现VS/Qt工程源码的一键备份
|
IDE JavaScript 编译器
原创:提高Qt Creator编译速度的7种方法,亲测可行(★firecat推荐★)
原创:提高Qt Creator编译速度的7种方法,亲测可行(★firecat推荐★)
1421 0
|
编译器
Qt Creator plugin动手实践(5)分享一个简化版的插件框架,qt-creator-minimal
Qt Creator plugin动手实践(5)分享一个简化版的插件框架,qt-creator-minimal
356 0
Qt Creator plugin动手实践(5)分享一个简化版的插件框架,qt-creator-minimal