boost库常用功能
2015-07-23 23:06
357 查看
1、shared_ptr
shared_ptr除了最基本的可以用new初始化以外,还可以使用其他方式初始化。在使用一些c的api时候,这种初始化方式非常有用,如下上面这段代码用来初始化一个curl的shared_ptr。
2、promise,future
future和promise配合可以应用在各种多线程环境下。
比如有些异步api(如zookeeper的watch),提高了编程难度。当我们想要并不要求效率,或者对时序有要求时,可以使用promise和future将这些异步api改为同步
3、regex
match
search
regex_token_iterator
stringcontent="bbbaaaaacccaaaaddddaaaeeeaaa";
//第四个参数0表示匹配到的整个字符串,1以后表示括号中的index,-1表示匹配除了本字符串以外的,可以用来分割字符串
boost::sregex_token_iteratoriter(content.begin(),content.end(),r,-1);
boost::sregex_token_iteratorend;
for(;iter!=end;++iter)
{
cout<<*iter<<endl;
}
regex_replace
public:
func(vector<string>vec){
_vec=vec;
_index=0;
}
stringaaa(boost::match_results<std::string::const_iterator>match){
stringaa=_vec[_index];
cout<<_index<<endl;
_index++;
cout<<_index<<endl;
returnaa;
}
private:
vector<string>_vec;
int_index;
};
intmain(intargc,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::strings="a?b?bc?d?e?";
std::stringb="a?b?bc?d?e?";
std::cout<<s<<std::endl;
boost::regexreg("\\?");
funcf(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;
return0;
}
4、path
stringmy_path="/";
bf::pathfile_path(my_path);
bf::directory_iteratorend_iter;
//遍历目录下的文件
for(bf::directory_iteratorfile_itr(file_path);file_itr!=end_iter;++file_itr)
{
stringfname=file_itr->path().filename().string();
cout<<fname<<endl;
}
相关文章推荐
- 股票学习(技术分析05)
- 文件打包与解压缩
- java RMI
- Java Beans
- *HDU 5305 - Friends (DFS + 剪枝)
- 第一百一十二天 how can I 坚持
- Nginx 笔记与总结(3)配置虚拟主机
- SUSE Linux源码编译安装MySQL 5.6
- Android代码优化Android Support Annotations
- hdu5305 Friends(dfs+map/hash)
- java高精度
- LoadRunner函数详解之web_convert_param
- 条件随机场
- Struts2的优化
- Hard to Play
- 使用传入的Model对象创建ModelAndView对象
- iOS 在UILabel显示不同的字体和颜色
- java中常用的字符串的截取方法 (2011-03-17 00:41:31)
- iOS开发笔记(3)---- 3DES/MD5加解密
- 学习CSS(8)