boost常用库案例
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()));
- }
完
boost常用库案例的更多相关文章
- boost常用库(一):boost数值转换
在STL中有一些字符转换函数,例如atoi,itoa等,在boost里面只需用一个函数lexical_cast进行转换,lexical_cast是模板方法,使用时需要传入类型.只能是数值类型转字符串. ...
- Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答
Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复 Boo ...
- 转:不应该不知道C++的常用库
不应该不知道C++的常用库 非常惭愧,我过去也仅仅了解boost.STLport这样的库,以及一些GUI库,但是居然有如此众多的C++库,其实令我惊讶.当然,这个问题应该辩证的看,对于拿来主义确实可以 ...
- iPhone开发 - 常用库
iPhone开发 - 常用库 这里总结了iPhone开发者开发过程中可能需要的一些资源 如何用Facebook graphic api上传视频: http://developers.facebook. ...
- Boost::thread库的使用
阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...
- [C++] C++中的常用库
转载自:C++常用库 C++ 资源大全 关于 C++ 框架.库和资源的一些汇总列表,内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包 ...
- 前端Demo常用库文件链接
<!doctype html><html><head> <meta charset="UTF-8"> <title>前端 ...
- chart.js图表库案例赏析,饼图添加文字
chart.js图表库案例赏析,饼图添加文字 Chart.js 是一个令人印象深刻的 JavaScript 图表库,建立在 HTML5 Canvas 基础上.目前,它支持6种图表类型(折线图,条形图, ...
- 如何在WINDOWS下编译BOOST C++库 .
如何在WINDOWS下编译BOOST C++库 cheungmine 2008-6-25 写出来,怕自己以后忘记了,也为初学者参考.使用VC8.0和boost1.35.0. 1)下载boost ...
随机推荐
- script ajax / XHR / XMLHttpRequest
s 利用XHR 调试发送form data表单数据,F5键刷新form表单URL ,http请求地址,获取token,提交. 如:http://pcp.cns*****.com/spcp-web/vm ...
- linux查看IP
1:输入 ifconfig,出现如下信息,找到eno16777736(网卡ip信息的配置文件名) 2:输入 cd /etc/sysconfig/network-scripts 找到网卡ip信息的配置文 ...
- 【1】【leetcode-79】 单词搜索
(典型dfs,知道思想写错) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单 ...
- HDU 1035(走迷宫 模拟)
题意是给定初始位置在一个迷宫中按照要求前进,判断多少步能离开迷宫或者多少步会走入一个长达多少步的循环. 按要求模拟前进的位置,对每一步在 vis[ ] 数组中进行已走步数的记录,走出去或走到已走过的位 ...
- ArcGis Python脚本——批量对影像、要素类定义投影
这一段是批量定义要素类(FeatureClasses)投影的ArcPy代码: 把要处理的要素类塞进一个文件夹(工作空间,workspace),然后将代码开头的路径换成这个“文件夹”的路径,处理完后再做 ...
- oldboy s21day01
1.操作系统的作用? 人操作软件,软件控制操作系统,操作系统控制硬件.2.列举你听过的操作系统及区别? 1.Windows 7/8/10 付费,操作方便,长时间运行卡顿. 2.Linux(Ubuntu ...
- Atlassian - Confluence Security Advisory - 2019-03-20
-------------------- This problem refers to the advisory found at https://confluence.atlassian.com/d ...
- luogu P3899 [湖南集训]谈笑风生
传送门 nmyzd,mgdhls,bnmbzdgdnlql,a,wgttxfs 对于一个点\(a\),点\(b\)只有可能是他的祖先或者在\(a\)子树里 如果点\(b\)是\(a\)祖先,那么答案为 ...
- 配置JDK和Tomcat环境变量
配置JDK和Tomcat环境变量 一.安装JDK和Tomcat 安装JDK:直接运行jdk-7-windows-i586.exe可执行程序,默认安装即可. 备注:路径可以其他盘符,不建议路径包含中文名 ...
- RSA加解密-2
Java使用RSA加密解密签名及校验 package com.ihep; import java.io.BufferedReader; import java.io.BufferedWriter; ...