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

简介:

1.boost::any

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

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

 

 #include  < boost / any.hpp > 
 
void  test_any()
 {
 typedef std::vector
 < boost::any >  many;
 many a;
 a.push_back(
 2 );
 a.push_back(
 string ( " test " ));

 
 for (unsigned  int  i = 0 ;i < a.size(); ++ i)
 
 {
  cout
 << a[i].type().name() << endl;
  
 try 
   
 {
   
 int  result  =  any_cast < int > (a[i]);
   cout
 << result << endl;
  }
 

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

 }
 

}
 

 

2.boost::array

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

 

 #include  < boost / array.hpp > 
 
void  test_array()
 {
 array
 < int , 10 >  ai  =   { 1 , 2 , 3 } ;

 
 for (size_t i = 0 ;i < ai.size(); ++ i)
 
 {
  cout
 << ai[i] << endl;
 }
 

}
 

 

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

 

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

 

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

 

 #include  < boost / format.hpp > 
 
void  test_format()
 {
 cout 
 <<  boost::format( " writing %1%,  x=%2% : %3%-th try "  %   " toto "   %   40.23   %   50   << endl; 

 format f(
 " a=%1%,b=%2%,c=%3%,a=%1% " );
 f 
 %   " string "   %   2   %   10.0 ;

 cout 
 <<  f.str()  <<  endl;
}
 

 

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

 

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


 


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

 

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

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

 

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

 

 #include  < boost / archive / text_oarchive.hpp > 
#include  < boost / archive / text_iarchive.hpp > 
#include  < boost / archive / xml_oarchive.hpp > 
  void  test_serialization()
  {
 boost::archive::text_oarchive to(cout , boost::archive::no_header);
  int  i  =   10 ;
  string  s  =   " This is a test\n " ;
 to  &  i;
 to  &  s;

 ofstream f( " test.xml " );
 boost::archive::xml_oarchive xo(f);
 xo  &  BOOST_SERIALIZATION_NVP(i)  &  BOOST_SERIALIZATION_NVP(s);

 boost::archive::text_iarchive ti(cin , boost::archive::no_header);
 ti  &  i  &  s;
 cout  << " i= " <<  i  <<  endl;
 cout  << " s= " <<  s  <<  endl;

 

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

 

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

 
 
struct  test
 {
 
 int  foo( int  x, int  y)
 
 {
  cout
 <<   " (test::foo invoking)x =  " <<  <<   "  y =  " <<  y  << endl;
  
 return  x + y;
 }
 

}
 
;

 void  test_function()
 {
 boost::function
 < int  ( int , int ) >  f;
 f 
 =  foo;
 cout 
 <<   " f(2,3)= " << f( 2 , 3 ) << endl;

 test x;
 
 /* f = std::bind1st(
      std::mem_fun(&test::foo), &x);
 */ 

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

 

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

 #include  < boost / shared_ptr.hpp > 
  class  Shared
  {
 public :
 Shared()
  {
  cout  <<   " ctor() called " << endl;
 } 

 Shared( const  Shared  &  other)
  {
  cout  <<   " copy ctor() called " << endl;
 } 

  ~ Shared()
  {
  cout  <<   " dtor() called " << endl;
 } 

 Shared  &   operator   =  ( const  Shared  &  other)
  {
  cout  <<   " operator =  called " << endl;
 } 

;

  void  test_shared_ptr()
  {
 typedef boost::shared_ptr < Shared >  SharedSP;
 typedef vector < SharedSP >  VShared;
 VShared v;
 v.push_back(SharedSP( new  Shared()));
 v.push_back(SharedSP( new  Shared()));
 
目录
相关文章
|
开发框架 Linux C语言
C、C++、boost、Qt在嵌入式系统开发中的使用
C、C++、boost、Qt在嵌入式系统开发中的使用
654 1
|
消息中间件 Linux API
C/C++ 进程间通信system V IPC对象超详细讲解(系统性学习day9)
C/C++ 进程间通信system V IPC对象超详细讲解(系统性学习day9)
|
编解码 C++ Python
成功解决LINK : fatal error LNK1181: 无法打开输入文件“avdevice.lib” error: command 'D:\\Program Files (x86)\\Micr
成功解决LINK : fatal error LNK1181: 无法打开输入文件“avdevice.lib” error: command 'D:\\Program Files (x86)\\Micr
成功解决LINK : fatal error LNK1181: 无法打开输入文件“avdevice.lib” error: command 'D:\\Program Files (x86)\\Micr
|
5月前
|
缓存 开发工具 git
QLExpress使用及源码分析
QLExpress是阿里开源的轻量级规则引擎,支持通过YAML配置表达式规则。支持AST语法树解析、上下文构建与动态执行,提供脚本缓存、别名映射、指令重写等扩展能力,适用于灵活的业务逻辑解耦与动态化控制。
|
存储 网络协议 前端开发
基于开源IM即时通讯框架MobileIMSDK:RainbowChat v11.7版已发布
Android端主要更新内容: 1)[优化] 优化了首页“消息”列表中单聊类型未正确同步时的收发消息和点击后的处理逻辑; 2)[优化] 优化了首页“消息”列表中同一好友和陌生人会话不能自动合并的问题;
473 2
|
网络协议 Linux 网络安全
微软工程师偷偷在用!这款SSH工具让Windows操控CentOS比Mac还优雅!
远程登录Linux服务器是管理和维护服务器的重要手段,尤其在远程办公、云服务管理等场景中不可或缺。通过工具如XShell,用户可以方便地进行远程管理。SSH协议确保了数据传输的安全性,命令行界面提高了操作效率。配置XShell连接CentOS时,需确保Linux系统开启sshd服务和22端口,并正确设置主机地址、用户名和密码。此外,调整字体和配色方案可优化使用体验,解决中文显示问题。
600 22
微软工程师偷偷在用!这款SSH工具让Windows操控CentOS比Mac还优雅!
|
存储 开发框架 开发工具
Electron有哪些使用场景
【10月更文挑战第13天】Electron有哪些使用场景
1185 0
|
存储 机器学习/深度学习 人工智能
【AI系统】昇腾 AI 处理器
本文介绍华为昇腾AI处理器的架构与卷积加速原理,基于达芬奇架构设计,支持云边端一体化解决方案,具备高能效比和强大的3D Cube矩阵计算单元。文章详细解析了昇腾AI处理器的核心组件及其高效的数据处理机制,旨在通过软硬件优化实现高效的卷积计算加速。
1764 2
|
机器学习/深度学习 数据采集 算法框架/工具
使用Python实现智能生态系统监测与保护的深度学习模型
使用Python实现智能生态系统监测与保护的深度学习模型
515 4
|
数据可视化 jenkins 持续交付
Jenkins Blue Ocean
Jenkins Blue Ocean
305 0