C++ 将BGL迭代顶点替换为;纯粹的;C++;11备选方案?
我想用纯C++11等价物替换顶点或边上的BGL迭代。BGL代码(从:)为: 或者类似于:C++ 将BGL迭代顶点替换为;纯粹的;C++;11备选方案?,c++,boost,c++11,iterator,boost-graph,C++,Boost,C++11,Iterator,Boost Graph,我想用纯C++11等价物替换顶点或边上的BGL迭代。BGL代码(从:)为: 或者类似于: 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) {...} 有可能吗?在外边缘上用一个简单的包装器就足够了: #include <boost/range/iterator_ran
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)
{...}
有可能吗?在
外边缘上用一个简单的包装器就足够了:
#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);
}
然后
for(auto e : pair_range(out_edges(v, g))){
// ...
}
Graph还提供了类似于Boost\u FOREACH
的方便宏,但专门为Graph迭代而设计
给定图形的所有顶点/边上的迭代由宏BGL\u FORALL\u顶点
/BGL\u FORALL\u边
及其模板对应项BGL\u FORALL\u顶点
/BGL\u FORALL\u边
提供
给定顶点的内外边上的迭代由宏BGL\u FORALL\u OUTEDGES
或BGL\u FORALL\u OUTEDGES
提供。(为其模板版本添加_T)。对于邻接顶点,请使用BGL\u For all\u ADJ
例如:
#include <boost/graph/iteration_macros.hpp>
typedef ... Graph;
Graph g;
BGL_FORALL_VERTICES(v, g, Graph) //v is declared here and
{ //is of type Graph::vertex_descriptor
BGL_FORALL_OUTEDGES(v, e, g, Graph) //e is declared as Graph::edge_descriptor
{
}
}
#包括
类型定义。。。图表
图g;
BGL_for所有_顶点(v,g,Graph)//v在这里声明,并且
{//属于Graph::vertex_描述符类型
BGL_FORALL_OUTEDGES(v,e,g,Graph)//e声明为Graph::edge_描述符
{
}
}
宏在C++03和C++11中都可以工作。谢谢,但是您提供的代码甚至都没有编译:(请查看源代码(包括您的代码):它们在这里:(我在第20行添加了“;”,因为没有它时缺少分号语法错误)@danilo2:机器人刚刚修复了我的答案,它在RemoveReference
中使用-alias缺少了一个简单的
。谢谢你,现在它可以编译了,但是这样使用时:for(auto&e:out_edges_range(v,g)){}我还有一个错误:@danilo2:我刚刚注意到问题中的e
也是一个简单的值-只需对(auto e:out\u edges\u range(v,g))
执行即可。问题是out\u edge\u迭代器在取消引用时按值返回,它们就是所谓的代理迭代器。
for(auto e : pair_range(out_edges(v, g))){
// ...
}
#include <boost/graph/iteration_macros.hpp>
typedef ... Graph;
Graph g;
BGL_FORALL_VERTICES(v, g, Graph) //v is declared here and
{ //is of type Graph::vertex_descriptor
BGL_FORALL_OUTEDGES(v, e, g, Graph) //e is declared as Graph::edge_descriptor
{
}
}