码迷,mamicode.com
首页 > 其他好文 > 详细

用BOOST_FOREACH简化遍历操作

时间:2016-02-29 19:35:30      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

BOOST_FOREACH能够方便的遍历STL容器.

仅仅须要头文件:

#include <boost/foreach.hpp>

然后遍历容器vector/list/set/deque/stack/queue都是类似的:

vector<int32_t> _v;
BOOST_FOREACH(int32_t value,_v)
{
//这里就能够訪问value
}

同一时候元素还支持引用,const,比方上面代码还能够写成:

vector<int32_t> _v;

BOOST_FOREACH(int32_t
& value,_v)
{
//这里就能够改动/訪问value
}

假设元素内容是结构体之类,引用能够防止拷贝~~

对于map的訪问有一点特殊,由于map的元素是std::pair<T1,T2>,所以须要写成这样:

技术分享
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;
}
技术分享

multimap么临时还没用过,只是相信也是类似的...感觉multimap有一点类似于map<key,set<value> > :-D

BOOST_FOREACH是正向的迭代,逆向的是BOOST_REVERSE_FOREACH。

看看BOOST_FOREACH的实现吧:

///////////////////////////////////////////////////////////////////////////////
// BOOST_FOREACH
//
//   For iterating over collections. Collections can be
//   arrays, null-terminated strings, or STL containers.
//   The loop variable can be a value or reference. For
//   example:
//
//   std::list<int> int_list(/*stuff*/);
//   BOOST_FOREACH(int &i, int_list)
//   {
//       /* 
//        * loop body goes here.
//        * i is a reference to the int in int_list.
//        */
//   }
//
//   Alternately, you can declare the loop variable first,
//   so you can access it after the loop finishes. Obviously,
//   if you do it this way, then the loop variable cannot be
//   a reference.
//
//   int i;
//   BOOST_FOREACH(i, int_list)
//       { ... }
//
#define BOOST_FOREACH(VAR, COL)                                                                 /
    BOOST_FOREACH_PREAMBLE()                                                                    /
    if (boost::foreach_detail_::auto_any_t _foreach_col = BOOST_FOREACH_CONTAIN(COL)) {} else   /
    if (boost::foreach_detail_::auto_any_t _foreach_cur = BOOST_FOREACH_BEGIN(COL)) {} else     /
    if (boost::foreach_detail_::auto_any_t _foreach_end = BOOST_FOREACH_END(COL)) {} else       /
    for (bool _foreach_continue = true;                                                         /
              _foreach_continue && !BOOST_FOREACH_DONE(COL);                                    /
              _foreach_continue ?

BOOST_FOREACH_NEXT(COL) : (void)0)                            /
        if  (boost::foreach_detail_::set_false(_foreach_continue)) {} else                      /
        for (VAR = BOOST_FOREACH_DEREF(COL); !_foreach_continue; _foreach_continue = true)

#endif

//代码一共同拥有800多行。我列出了最后的凝视和定义。

我认为BOOST_FOREACH有点搞过头了。手写for的循环。最多也就两行。为什么要为形式上的简单而引入如此多的定义和编译器解析。并且这还是个宏。我不是反对宏,仅仅是认为宏在这个地方没带来太多优点,反而添乱。调试的噩梦,郁闷死。

不是每一个库都是那么精彩和有用的。std::vector<bool>的特化。std::auto_ptr的设计也都不那么让人舒适。

保持脑袋清醒,有选择的使用类库吧。

最后。还是要感叹一下BOOST_FOREACH实现。太牛了。



用BOOST_FOREACH简化遍历操作

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/5228595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!