博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
boost库的常用组件的使用(ZT)
阅读量:7237 次
发布时间:2019-06-29

本文共 3822 字,大约阅读时间需要 12 分钟。

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}  

转载地址:http://qdwbm.baihongyu.com/

你可能感兴趣的文章
react-native-storage + AsyncStorage 实现数据存储
查看>>
Cobaltstrike、armitage联动
查看>>
pandas set_index和reset_index的用法
查看>>
[Bash] View Files and Folders in Bash
查看>>
PEACHPIE 0.9.11 版本发布,可以上生产了
查看>>
异常检测——局部异常因子(Local Outlier Factor ,LOF)算法
查看>>
记录一次广州白云区项目数据库连接失败问题的解决过程
查看>>
干货:Vue粒子特效(vue-particles插件)
查看>>
Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象...
查看>>
加密PDF为只读模式
查看>>
让你编写的控件库在 XAML 中有一个统一的漂亮的命名空间(xmlns)和命名空间前缀...
查看>>
MySQL数据库的锁详解【转】
查看>>
ip route 解释
查看>>
【转】Android中保持Service的存活
查看>>
Consul功能简介
查看>>
IdentityServer4实战 - API与IdentityServer的交互过程解析
查看>>
Delphi编程 -- 使用CPUID指令获取CPU信息(转自大富翁)
查看>>
Android setRequestedOrientation用法
查看>>
面向对象三大基本特性,五大基本原则
查看>>
更改窗口图标并将其显示在任务栏
查看>>