更换BGL遍历顶点与"纯QUOT; C ++ 11的替代? [英] Replace BGL iterate over vertexes with "pure" C++11 alternative?

查看:127
本文介绍了更换BGL遍历顶点与"纯QUOT; C ++ 11的替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要替换的BGL迭代结束顶点或用纯C ++ 11相当于边缘。 BGL的code(来自:<一href=\"http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/quick_tour.html\">http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/quick_tour.html)是:

I want to replace the BGL iteration over vertexes or edges with pure C++11 equivalent. The BGL code (from: http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/quick_tour.html) is:

typename boost::graph_traits<Graph>::out_edge_iterator out_i, out_end;
typename boost::graph_traits<Graph>::edge_descriptor e;
for (std::tie(out_i, out_end) = out_edges(v, g);
     out_i != out_end; ++out_i)
{
  e = *out_i;
  Vertex src = source(e, g), targ = target(e, g);
  std::cout << "(" << name[get(vertex_id, src)]
            << "," << name[get(vertex_id, targ)] << ") ";
}

我试了几个建议,从这里开始:<一href=\"http://stackoverflow.com/questions/10523648/replace-boost-foreach-with-pure-c11-alternative\">Replace BOOST_FOREACH与&QUOT;纯QUOT; C ++ 11的替代?但没有运气。

我希望能够写类似:

for (auto &e : out_edges(v, g))
{ ... }

或类似的东西:

for (std::tie(auto out_i, auto out_end) = out_edges(v, g);
     out_i != out_end; ++out_i)
{...}

这可能吗?

推荐答案

一个简单的包装在 out_edges 应该足够了:

A simple wrapper over out_edges should suffice:

#include <boost/range/iterator_range.hpp>
#include <type_traits>

template<class T> using Invoke = typename T::type
template<class T> using RemoveRef = Invoke<std::remove_reference<T>>;
template<class G> using OutEdgeIterator = typename boost::graph_traits<G>::out_edge_iterator;

template<class V, class G>
auto out_edges_range(V&& v, G&& g)
  -> boost::iterator_range<OutEdgeIterator<RemoveRef<G>>>
{
  auto edge_pair = out_edges(std::forward<V>(v), std::forward<G>(g));
  return boost::make_iterator_range(edge_pair.first, edge_pair.second);
}

或者更简单,即把一个函数的std ::对进入有效范围:

template<class It>
boost::iterator_range<It> pair_range(std::pair<It, It> const& p){
  return boost::make_iterator_range(p.first, p.second);
}

然后

for(auto e : pair_range(out_edges(v, g))){
  // ...
}

这篇关于更换BGL遍历顶点与&QUOT;纯QUOT; C ++ 11的替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆