1.BOOST_FOREACH
经常会遍历容器,写for/while循环到手痛,使用BOOST_FOREACH可以减少我们的工作。支持容器vector/list/set/deque/stack/queue
#include <boost/foreach.hpp> BOOST_FOREACH(value,container){ //遍历每个元素 }
例子:
vector<int32_t> _v; BOOST_FOREACH(int32_t& value,_v) { //这里就可以修改/访问value } std::map<int32_t,int32_t> _map; typedef const std::map<int32_t, int32_t>::value_type const_pair; BOOST_FOREACH(const_pair& node,_map) { //这里就可以访问node的元素 int32_t key = node.first; int32_t value = node.second; }
2.字符串切割
#include <boost/tokenizer.hpp> int split(const string& str, const string& strSep,vector<string>& vec) { typedef boost::tokenizer<boost::char_separator<char> > tokenizer; boost::char_separator<char> sep(strSep.c_str()); tokenizer token(str, sep); tokenizer::iterator it = token.begin(); for (; it != token.end(); ++it) { vec.push_back(*it); } return vec.size(); } </char></char></string>