boost库常用功能
1、shared_ptr
shared_ptr除了最基本的可以用new初始化以外,还可以使用其他方式初始化。在使用一些c的api时候,这种初始化方式非常有用,如下
boost::shared_ptr<CURL> curl_(curl_easy_init(), curl_easy_cleanup);
上面这段代码用来初始化一个curl的shared_ptr。
2、promise,future
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
#include <iostream> using namespace std; int main()
{
boost::promise<int> pi;
boost::unique_future<int> fi;
fi=pi.get_future();
//等待2s,超时返回0,被promise被set返回1
cout << "wait over:" << fi.timed_wait(boost::posix_time::milliseconds(2000)) << endl;
//检查promise是否被set
cout << "is ready:" << fi.is_ready() << endl; pi.set_value(42);
cout << "wait over:" << fi.timed_wait(boost::posix_time::milliseconds(2000)) << endl;
cout << "is ready:" << fi.is_ready() << endl;
cout << fi.get() << endl;
} 上面代码运行结果为

future和promise配合可以应用在各种多线程环境下。
比如有些异步api(如zookeeper的watch),提高了编程难度。当我们想要并不要求效率,或者对时序有要求时,可以使用promise和future将这些异步api改为同步
int main()
{
boost::promise<int> pi;
boost::unique_future<int> fi = pi.get_future();
char content[1024] = "\0";
int size = 1024;
zoo_wget(zk_handle, path, callback, static_cast<void*>(&pi), content, &size, NULL));
fi.get();
} void callback(zhandle_t* zh, int type, int state, const char* path, void* watcherCtx)
{
boost::promise<int>* pi = static_cast<boost::promise<int>*>(watcherCtx);
pi->set_value(1);
}
3、regex
match
boost::regex r("(a*)ddd(b*)ddd");
boost::smatch m;
if(boost::regex_match(string("aaaadddbbbddd"), m, r))
{
cout << m.size() << endl;
for(size_t i = 0; i < m.size(); ++i)
{
//0表示匹配到的整个字符串,1以后的index表示括号中匹配的内容
cout << m[i] << endl;
}
}
search
boost::regex r("(a+)");
string content = "bbbaaaaacccaaaaddddaaaeeeaaa";
boost::smatch m;
string::const_iterator strstart = content.begin();
string::const_iterator strend = content.end();
while(boost::regex_search(strstart, strend, m, r))
{
//search到的结果,0表示整个,1以后表示括号的index匹配的结果
cout << m[1] << endl;
//从上次搜索到的地方接着搜索
strstart = m[0].second;
}
regex_token_iterator
boost::regex r("(a)c");
string content = "bbbaaaaacccaaaaddddaaaeeeaaa";
//第四个参数0表示匹配到的整个字符串,1以后表示括号中的index,-1表示匹配除了本字符串以外的,可以用来分割字符串
boost::sregex_token_iterator iter(content.begin(), content.end(), r, -1);
boost::sregex_token_iterator end;
for(; iter != end; ++ iter)
{
cout<< *iter << endl;
}
regex_replace
class func {
public:
func(vector<string> vec){
_vec = vec;
_index = 0;
}
string aaa(boost::match_results<std::string::const_iterator> match){
string aa = _vec[_index];
cout <<_index<<endl;
_index++;
cout <<_index<<endl;
return aa;
}
private:
vector<string> _vec;
int _index;
};
int main(int argc, char* argv[])
{
vector<string> test;
test.push_back("1");
test.push_back("2");
test.push_back("3");
test.push_back("4");
test.push_back("5");
test.push_back("6");
test.push_back("7");
std::string s="a ? b ?b c? d? e?";
std::string b="a ? b ?b c? d? e?";
std::cout << s << std::endl;
boost::regex reg("\\?");
func f(test);
boost::function<std::string (boost::match_results<std::string::const_iterator>)> function1 =
boost::bind(&func::aaa, &f, _1);
//替换s中的所有符合r的字符串
//第三个参数可以放一个函数对象或者boost::function类型,用来实现特殊逻辑
s=boost::regex_replace(s,reg,function1);
std::cout << s << std::endl;
return 0;
}
4、path
namespace bf = boost::filesystem;
string my_path = "/";
bf::path file_path(my_path);
bf::directory_iterator end_iter;
//遍历目录下的文件
for (bf::directory_iterator file_itr(file_path); file_itr != end_iter; ++file_itr)
{
string fname = file_itr->path().filename().string();
cout << fname << endl;
}
boost库常用功能的更多相关文章
- c++学习书籍推荐《超越C++标准库:Boost库导论》下载
<超越C++标准库Boost库导论>不仅介绍了Boost库的功能.使用方法及注意事项,而且还深入讨论了Boost库的设计理念.解决问题的思想和技巧以及待处理的问题.因此,本书是一本了解Bo ...
- 漫步Facebook开源C++库Folly之string类设计(散列、字符串、向量、内存分配、位处理等,小部分是对现有标准库和Boost库功能上的补充,大部分都是基于性能的需求而“重新制造轮子”)
就在近日,Facebook宣布开源了内部使用的C++底层库,总称folly,包括散列.字符串.向量.内存分配.位处理等,以满足大规模高性能的需求. 这里是folly的github地址:https:// ...
- iOS项目开发常用功能静态库
YHDeveloperTools iOS项目开发常用功能静态库 查看源码 功能方法: 1.字符检查 [NSString checkStringWithType:Email andTargetStrin ...
- [C/C++] C/C++延伸学习系列之STL及Boost库概述
想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...
- (九)boost库之文件处理filesystem
(九)boost库之文件处理filesystem filesystem库是一个可移植的文件系统操作库,它在底层做了大量的工作,使用POSIX标准表示文件系统的路径,使C++具有了类似脚本语言的功能 ...
- Boost库学习之旅入门篇
学习及使用Boost库已经有一段时间了,Boost为我的日常开发中带来了极大的方便,也使得我越来越依赖于boost库了.但boost功能太多,每次使用还是得翻看以前的 资料,所以为了以后可以更方便的使 ...
- boost库的安装,使用,介绍,库分类
1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...
- 详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
一.boost 智能指针 智能指针是利用RAII(Resource Acquisition Is Initialization:资源获取即初始化)来管理资源.关于RAII的讨论可以参考前面的文章.在使 ...
- JavaScript 常用功能总结
小编吐血整理加上翻译,太辛苦了~求赞! 本文主要总结了JavaScript 常用功能总结,如一些常用的JS 对象,基本数据结构,功能函数等,还有一些常用的设计模式. 目录: 众所周知,JavaScri ...
随机推荐
- 应用程序已被Java安全阻止
提示 您的安全设置已阻止自签名的应用程序运行 控制面板-JAVA-安全-例外站点-https://域名或IP/或http://域名或IP/,注意结尾必须要加/否则还是会一直提示被阻止
- 你不一定知道的、并没有什么卵用的一些python库
1. delorean,用来处理时间的库 import datetime import pytz # 一般情况下,我们想表示时间的话 est = pytz.timezone("Asia/Sh ...
- (3) python--matplotlib
(一)1.如何绘制散点图 import numpy as np import matplotlib.pyplot as plt # 如何绘制散点图 # 先随机生成数据 x = np.array(ran ...
- linux日志服务之logwatch
因为logwatch默认要使用sendmail服务,所以请参考linux之发送邮件--sendmail服务配置首先设置正确sendmail服务. 安装logwatch. 查看logwatch文件在/e ...
- [BZOJ2223][BZOJ3524][Poi2014]Couriers 主席树
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2436 Solved: 960[Submit][St ...
- UpdateLayeredWindow后,使用Gdi DrawText文字透明的解决办法
来源:http://stackoverflow.com/questions/5309914/updatelayeredwindow-and-drawtext 要点就是在先在memDc DrawText ...
- Python的程序结构[0] -> 属性/Property[0] -> 类属性、实例属性和私有属性
类属性.实例属性和私有属性 Python中类的属性主要包括类属性,实例属性和私有属性,下面是对三种属性的简单介绍 类属性 / Class Property 类属性在__init__()之外初始化,在外 ...
- luogu P1417 烹调方案
题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...
- 在mac上运行android的intel模拟器导致的死机问题解决
最近用Mac开发android的时候遇到了一个烦人的问题,装上了HAXM,intel hardware accelerated execution manager以后,一运行android emula ...
- 当ASP.NET Forms验证方式遭遇苹果IOS
一.问题出现 我在用ASP.NET MVC4做微信开发的时候,用Forms验证方式做为authentication. 一般都是在web.config加: <authentication mode ...