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. }

boost常用库案例的更多相关文章

  1. boost常用库(一):boost数值转换

    在STL中有一些字符转换函数,例如atoi,itoa等,在boost里面只需用一个函数lexical_cast进行转换,lexical_cast是模板方法,使用时需要传入类型.只能是数值类型转字符串. ...

  2. Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答

    Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复   Boo ...

  3. 转:不应该不知道C++的常用库

    不应该不知道C++的常用库 非常惭愧,我过去也仅仅了解boost.STLport这样的库,以及一些GUI库,但是居然有如此众多的C++库,其实令我惊讶.当然,这个问题应该辩证的看,对于拿来主义确实可以 ...

  4. iPhone开发 - 常用库

    iPhone开发 - 常用库 这里总结了iPhone开发者开发过程中可能需要的一些资源 如何用Facebook graphic api上传视频: http://developers.facebook. ...

  5. Boost::thread库的使用

    阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...

  6. [C++] C++中的常用库

    转载自:C++常用库 C++ 资源大全 关于 C++ 框架.库和资源的一些汇总列表,内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包 ...

  7. 前端Demo常用库文件链接

    <!doctype html><html><head> <meta charset="UTF-8"> <title>前端 ...

  8. chart.js图表库案例赏析,饼图添加文字

    chart.js图表库案例赏析,饼图添加文字 Chart.js 是一个令人印象深刻的 JavaScript 图表库,建立在 HTML5 Canvas 基础上.目前,它支持6种图表类型(折线图,条形图, ...

  9. 如何在WINDOWS下编译BOOST C++库 .

    如何在WINDOWS下编译BOOST C++库 cheungmine 2008-6-25   写出来,怕自己以后忘记了,也为初学者参考.使用VC8.0和boost1.35.0.   1)下载boost ...

随机推荐

  1. UNIX网络编程中的字节序问题

    1.inet_pton 函数原型: inet_pton:将“点分十进制” -> “二进制整数” int inet_pton(int af, const char *src, void *dst) ...

  2. html基本进阶知识【转】

    inline和block的区别: 网页一般是两种元素组合起来的,一种是内联元素,也就是行内显示,加上width和height没效果.一种是区块元素,可以加上对应的width和height,通常使用在网 ...

  3. javasrcipt的作用域和闭包(二)续篇之:函数内部提升机制与Variable Object

    一个先有鸡还是先有蛋的问题,先看一段代码: a = 2; var a; console.log(a); 通常我们都说JavaScript代码是由上到下一行一行执行,但实际这段代码输出的结果是2.但这段 ...

  4. 【1】【leetcode-76】 最小覆盖子串

     最小覆盖子串(hard) (不会) 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", ...

  5. 07-查询操作(DQL)-多表查询

    一. 综述    查询操作主要从两个方面来说:单表查询和多表查询. 多表查询包括:笛卡尔积.外键约束.内连接查询.外链接查询.自连接查询. 二 . 案例设计   1.  设计产品表(product). ...

  6. Java9+版本中,Interface的内容

    使用接口的注意事项: 1.接口没有静态代码块或者构造方法 2.一个类的父类是唯一的,但是一个类可以同时实现多个接口(区别) 3.如果实现类实现多个接口有重名的抽象方法,那么实现类只需要覆盖重写一个即可 ...

  7. UUID在Java中的实现与应用

    UUID是什么 UUID的全称为:Universally Unique IDentifier,也被称为GUID(Globally Unique IDentifier).是一种由算法生成的唯一标识,它实 ...

  8. 【转载】详解KMP算法

    网址:https://www.cnblogs.com/yjiyjige/p/3263858.html

  9. instanceof 操作符实现原理解析

    本文会介绍ES6规范中 instanceof 操作符的实现,以及自定义 instanceof 操作符行为的几个方法. 文中涉及的规范相关的代码皆为伪代码,为了便于理解,其中可能会省略一些参数判断逻辑或 ...

  10. webpack-config.js 内容讲解

    当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面 (环境变量及其基本变量的配置) var path = require('path') m ...