Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将BGL迭代顶点替换为;纯粹的;C++;11备选方案?_C++_Boost_C++11_Iterator_Boost Graph - Fatal编程技术网

C++ 将BGL迭代顶点替换为;纯粹的;C++;11备选方案?

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

我想用纯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_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
     {

     }
}