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