天天看点

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

1.boost::any

boost::any是一种通用的数据类型,可以将各种类型包装后统一放入容器内

最重要的它是类型安全的。有点象COM里面的variant.

使用方法:

any::type() 返回包装的类型

any_cast可用于any到其他类型的转化

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

 #include  < boost / any.hpp > 

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

 void  test_any()

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

 {

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

 typedef std::vector < boost::any >  many;

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

 many a;

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

 a.push_back( 2 );

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

 a.push_back( string ( " test " ));

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

  for (unsigned  int  i = 0 ;i < a.size(); ++ i)

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

  {

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

  cout << a[i].type().name() << endl;

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

   try 

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

    {

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

    int  result  =  any_cast < int > (a[i]);

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

   cout << result << endl;

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

  } 

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

   catch (boost::bad_any_cast  &  ex)

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

   {

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

   cout << " cast error: " << ex.what() << endl;

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

 } 

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

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

2.boost::array

boost::array仅仅是对数组一层薄薄的封装,提供跟各种算法配合的iterator,使用方法很简单

注意:可以使用{}来初始化array,因为array所有的成员变量都是public的

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

 #include  < boost / array.hpp > 

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

 void  test_array()

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

 array < int , 10 >  ai  =   { 1 , 2 , 3 } ;

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

  for (size_t i = 0 ;i < ai.size(); ++ i)

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

  cout << ai[i] << endl;

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

3.boost::lexical_cast

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

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

 #include  < boost / lexical_cast.hpp > 

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

 void  test_lexical_cast()

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

  int  i  =  boost::lexical_cast < int > ( " 123 " );

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

 cout  <<  i  <<  endl;

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

4.boost::format 

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

而且还可以重复使用参数

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

 #include  < boost / format.hpp > 

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

 void  test_format()

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

 cout  <<  boost::format( " writing %1%,  x=%2% : %3%-th try " )  %   " toto "   %   40.23   %   50   << endl; 

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

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

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

 f  %   " string "   %   2   %   10.0 ;

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

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

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

5.boost::tokenizer

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

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

 #include  < boost / tokenizer.hpp > 

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

 void  test_tokenizer()

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

  string  s( " This is  , a ,test! " );

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

 boost::tokenizer <>  tok(s);

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

  for (tokenizer <> ::iterator beg = tok.begin(); beg != tok.end(); ++ beg) {

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

       cout  <<   * beg  <<   " \n " ;

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

6.boost::thread 

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

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

 #include  < boost / thread.hpp > 

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

 void  mythread()

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

 cout << " hello,thread! " << endl;

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

 void  test_thread()

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

 boost::function <   void  ()  >  f(mythread);

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

 boost::thread t(f);

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

 t.join();

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

 cout << " thread is over! " << endl;

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

7.boost::serialization

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

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

 #include  < boost / archive / text_oarchive.hpp > 

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

#include  < boost / archive / text_iarchive.hpp > 

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

#include  < boost / archive / xml_oarchive.hpp > 

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

 void  test_serialization()

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

 boost::archive::text_oarchive to(cout , boost::archive::no_header);

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

  int  i  =   10 ;

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

  string  s  =   " This is a test\n " ;

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

 to  &  i;

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

 to  &  s;

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

 ofstream f( " test.xml " );

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

 boost::archive::xml_oarchive xo(f);

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

 xo  &  BOOST_SERIALIZATION_NVP(i)  &  BOOST_SERIALIZATION_NVP(s);

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

 boost::archive::text_iarchive ti(cin , boost::archive::no_header);

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

 ti  &  i  &  s;

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

 cout  << " i= " <<  i  <<  endl;

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

 cout  << " s= " <<  s  <<  endl;

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

8.boost::function 

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

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

 #include  < boost / function.hpp > 

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

 int  foo( int  x, int  y)

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

 cout <<   " (foo invoking)x =  " << x  <<   "  y =  " <<  y  << endl;

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

  return  x + y;

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

 struct  test

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

  int  foo( int  x, int  y)

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

  cout <<   " (test::foo invoking)x =  " << x  <<   "  y =  " <<  y  << endl;

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

   return  x + y;

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

} ;

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

 void  test_function()

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

 boost::function < int  ( int , int ) >  f;

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

 f  =  foo;

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

 cout  <<   " f(2,3)= " << f( 2 , 3 ) << endl;

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

 test x;

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

  /* f = std::bind1st(

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

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

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

 boost::function < int  (test * , int , int ) >  f2;

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

 f2  =   & test::foo;

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

 cout  <<   " f2(5,3)= " << f2( & x, 5 , 3 ) << endl;

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

9.boost::shared_ptr 

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

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

 #include  < boost / shared_ptr.hpp > 

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

 class  Shared

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

 public :

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

 Shared()

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

  cout  <<   " ctor() called " << endl;

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

 Shared( const  Shared  &  other)

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

  cout  <<   " copy ctor() called " << endl;

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

  ~ Shared()

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

  cout  <<   " dtor() called " << endl;

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

 Shared  &   operator   =  ( const  Shared  &  other)

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

  cout  <<   " operator =  called " << endl;

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

 void  test_shared_ptr()

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

 typedef boost::shared_ptr < Shared >  SharedSP;

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

 typedef vector < SharedSP >  VShared;

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

 VShared v;

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

 v.push_back(SharedSP( new  Shared()));

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

}