boost常用库案例
原文地址:http://cjbskysea.blogbus.com/logs/61808617.html
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 << " ";
- }
- }
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 ";
- 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 = "<<x << " y = "<< y <<endl;
- return x+y;
- }
- struct test
- {
- int foo(int x,int y)
- {
- cout<< "(test::foo invoking)x = "<<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()));
- }
完
转载于:https://www.cnblogs.com/wangkangluo1/archive/2011/07/19/2110746.html
boost常用库案例相关推荐
- Python处理时空数据常用库案例及练习
本文完整代码.数据集下载.在线运行可以访问这个链接:时空数据Python常用包案例 配套习题与答案可以访问这个链接:时空数据Python常用包案例 - 实操练习题(附答案) Python处理时空数据会 ...
- Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答
Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复 Boost ...
- boost正则库匹配ASII编码的中文、全角字符示例
首先,boost正则库(regex)不支持形如 [0-9] 这样的表达式,貌似会崩溃. 现在查网上正则匹配中文的例子,都是讲用 \uFF00-\uFFFF ; 拜托,\u是unicode编码,能用于我 ...
- Python快速编程入门#学习笔记02# |第十章 :Python计算生态与常用库(附.小猴子接香蕉、双人乒乓球小游戏源码)
全文目录 学习目标 1. Python计算生态概述 1.1 Python计算生态概述 2. Python生态库的构建与发布 2.1 模块的构建与使用 * 2.1.1第三方库/模块导入的格式 2.2 包 ...
- boost网络库开发
一.前言 网络库是从事C++开发最基础.最核心.最常用的一个库,所有的协议都是建立在一个稳定高效的网络库之上的,所以对于c++程序员来说它是必不可少且非常非常重要的一个核心组件,我们可以使用网络库做任 ...
- Boost C++ 库
http://zh.highscore.de/cpp/boost/frontpage.html Boost C++ 库 目录 第 1 章 简介 第 2 章 智能指针 第 3 章 函数对象 第 4 章 ...
- python常用类库_Python常用库
Python常用库 一.time:时间处理模块 import time 1.time.time() time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数). import tim ...
- NumPy和Pandas常用库
NumPy和Pandas常用库 1.NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对整组数 ...
- 【Python学习系列三】Windows下Python第三方常用库安装
Python有丰富的第三方库,提供丰富的爬虫.数据分析等功能. 方法一:下载完整包,含编译器和常见库,https://www.continuum.io/downloads/ 针对操作 ...
最新文章
- 最近后缀.Scaletto,.com}KBK,com}BET,.Fuchsi勒索病毒开始爆发…
- tensorflow-Inception-v3模型训练自己的数据代码示例
- php ajax 注册,非常实用的ajax用户注册模块
- 云炬WEB开发笔记3-1 项目初始化概要
- linux java 查询mysql_Linux Java连接MySQL数据库
- SAP CRM和SAP Hybris的订单修改记录
- mac按文件名查找文件_如何在Mac上查找和删除大文件
- Arduino笔记-WeMos D1通过HTTP亮熄灯
- android 字体切换快捷键,Android stdio 字体设置及快捷键
- 高通增加Adsp log( 三十)
- 如何使用Magoshare Data Recovery在mac上恢复找回删除的丢失文件?
- linux log 2 1,51CTO博客-专业IT技术博客创作平台-技术成就梦想
- LinkedList的线程安全解决办法
- 从客户端登陆服务器的配置文件,从客户端登陆服务器的配置
- 电阻电容等封装对应功率
- 申请计算机语言学留学要求,布兰迪斯大学计算机语言学文学硕士研究生申请要求及申请材料要求清单...
- 计算机管理 存储空间不足,win10系统提示“存储空间不足无法处理此命令”的处理技巧...
- tasklist 结束进程_windows
- 在win8.1系统下运行VC++6.0
- java使用aspose将word,excel,ppt转pdf
热门文章
- json/ 发送形式_24/7的完整形式是什么?
- obj[]与obj._Ruby中带有示例的Array.rassoc(obj)方法
- pandas 根据列名索引多列数据_Pandas 数据聚合与分组运算[groupby+apply]速查笔记
- 计算机常用英语1000个,1000个常用英语单词.pdf
- java server模式 设置_JVM client模式和Server模式的区别
- linux线程学习初步02
- 全国英语计算机9月统考2019,2019年9月网络教育统考《计算机应用基础》模拟题6...
- linux网络编程(二)TCP通讯状态
- 【微机原理与接口技术】具体芯片(1)并行接口8255A(2):控制字概述
- #if/#else/#endif