1.boost::any

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

使用方法:

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

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

  1. #include <boost/any.hpp>
  2. void test_any()
  3. {
  4. typedef std::vector<boost::any> many;
  5. many a;
  6. a.push_back(2);
  7. a.push_back(string("test"));
  8. for(unsigned int i=0;i<a.size();++i)
  9. {
  10. cout<<a[i].type().name()<<endl;
  11. try
  12. {
  13. int result = any_cast<int>(a[i]);
  14. cout<<result<<endl;
  15. }
  16. catch(boost::bad_any_cast & ex)
  17. {
  18. cout<<"cast error:"<<ex.what()<<endl;
  19. }
  20. }
  21. }

2.boost::array

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

  1. #include <boost/array.hpp>
  2. void test_array()
  3. {
  4. array<int,10> ai = {1,2,3};
  5. for(size_t i=0;i<ai.size();++i)
  6. {
  7. cout<<ai[i]<<endl;
  8. }
  9. }

3.boost::lexical_cast

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

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

4.boost::format

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

  1. #include <boost/format.hpp>
  2. void test_format()
  3. {
  4. cout <<
  5. boost::format("writing %1%,  x=%2% : %3%-th try")
  6. % "toto"
  7. % 40.23
  8. % 50
  9. <<endl;
  10. format f("a=%1%,b=%2%,c=%3%,a=%1%");
  11. f % "string" % 2 % 10.0;
  12. cout << f.str() << endl;
  13. }

5.boost::tokenizer

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

  1. #include <boost/tokenizer.hpp>
  2. void test_tokenizer()
  3. {
  4. string s("This is  , a ,test!");
  5. boost::tokenizer<> tok(s);
  6. for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg)
  7. {
  8. cout << *beg << " ";
  9. }
  10. }

6.boost::thread

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

  1. #include <boost/thread.hpp>
  2. void mythread()
  3. {
  4. cout<<"hello,thread!"<<endl;
  5. }
  6. void test_thread()
  7. {
  8. boost::function< void () > f(mythread);
  9. boost::thread t(f);
  10. t.join();
  11. cout<<"thread is over!"<<endl;
  12. }

7.boost::serialization

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

  1. #include <boost/archive/text_oarchive.hpp>
  2. #include <boost/archive/text_iarchive.hpp>
  3. #include <boost/archive/xml_oarchive.hpp>
  4. void test_serialization()
  5. {
  6. boost::archive::text_oarchive to(cout , boost::archive::no_header);
  7. int i = 10;
  8. string s = "This is a test ";
  9. to & i;
  10. to & s;
  11. ofstream f("test.xml");
  12. boost::archive::xml_oarchive xo(f);
  13. xo & BOOST_SERIALIZATION_NVP(i) & BOOST_SERIALIZATION_NVP(s);
  14. boost::archive::text_iarchive ti(cin , boost::archive::no_header);
  15. ti & i & s;
  16. cout <<"i="<< i << endl;
  17. cout <<"s="<< s << endl;
  18. }

8.boost::function

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

  1. #include <boost/function.hpp>
  2. int foo(int x,int y)
  3. {
  4. cout<< "(foo invoking)x = "<<x << " y = "<< y <<endl;
  5. return x+y;
  6. }
  7. struct test
  8. {
  9. int foo(int x,int y)
  10. {
  11. cout<< "(test::foo invoking)x = "<<x << " y = "<< y <<endl;
  12. return x+y;
  13. }
  14. };
  15. void test_function()
  16. {
  17. boost::function<int (int,int)> f;
  18. f = foo;
  19. cout << "f(2,3)="<<f(2,3)<<endl;
  20. test x;
  21. /*f = std::bind1st(std::mem_fun(&test::foo), &x);*/
  22. boost::function<int (test*,int,int)> f2;
  23. f2 = &test::foo;
  24. cout << "f2(5,3)="<<f2(&x,5,3)<<endl;
  25. }

9.boost::shared_ptr

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

  1. #include <boost/shared_ptr.hpp>
  2. class Shared
  3. {
  4. public:
  5. Shared()
  6. {
  7. cout << "ctor() called"<<endl;
  8. }
  9. Shared(const Shared & other)
  10. {
  11. cout << "copy ctor() called"<<endl;
  12. }
  13. ~Shared()
  14. {
  15. cout << "dtor() called"<<endl;
  16. }
  17. Shared & operator = (const Shared & other)
  18. {
  19. cout << "operator =  called"<<endl;
  20. }
  21. };
  22. void test_shared_ptr()
  23. {
  24. typedef boost::shared_ptr<Shared> SharedSP;
  25. typedef vector<SharedSP> VShared;
  26. VShared v;
  27. v.push_back(SharedSP(new Shared()));
  28. v.push_back(SharedSP(new Shared()));
  29. }

最新文章

  1. Linux上的SQL Server——预告片
  2. Windows Store App JavaScript 开发:WinJS库控件
  3. javaSE第四天
  4. [Locked] Flatten 2D Vector
  5. ExtJs5_继承自定义一个控件
  6. BNUOJ 34981 A Matrix
  7. 结对作业--基于GUI的四则运算生成器
  8. Tempdb总结
  9. 使用Xamarin实现跨平台移动应用开发(转载)
  10. linux下的Shell编程(8)自定义函数
  11. 汇编实现: C库常见函数,串操作指令作用
  12. Python操作Memcached
  13. SQL语句(一)SQL和数据库数据表的创建
  14. 项目出现小红叉,类名上带有 Implicit错误
  15. file_get_post实现post请求
  16. 2018.07.06 BZOJ1208: HNOI2004宠物收养所(非旋treap)
  17. 有用的linux指令(资料转载)
  18. [ 原创 ] git使用技巧
  19. python中 @property
  20. python下操作redis

热门文章

  1. my live boadband
  2. thinkpad yoga 12 / thinkpad s1 yoga / WS860
  3. JDBC-DBCP
  4. hbase记录-备份脚本参考
  5. 微信、支付宝支付SDK
  6. mysql导入导出sql文件,source导入速度慢的解决办法
  7. threading.local学习
  8. TCP回射服务器修订版(ubuntu 18.04)
  9. HDB3 译码器
  10. applicationContext.xml配置Spring样板