c++ - Boost 图的自定义 InputIterator (BGL)

标签 c++ boost graph iterator

我有一个顶点和边具有自定义属性的图形。我现在想创建此图的拷贝,但我不希望顶点像原始图那样复杂。我的意思是,顶点具有与原始图中相同的索引 (vertex_index_t) 就足够了。
我不想手动复制,而是想使用 boost::adjacency_list 的复制功能(s. http://www.boost.org/doc/libs/1_37_0/libs/graph/doc/adjacency_list.html ):

template <class EdgeIterator>
adjacency_list(EdgeIterator first, EdgeIterator last,
           vertices_size_type n,
           edges_size_type m = 0,
           const GraphProperty& p = GraphProperty())

那里的描述说:

The EdgeIterator must be a model of InputIterator. The value type of the EdgeIterator must be a std::pair, where the type in the pair is an integer type. The integers will correspond to vertices, and they must all fall in the range of [0, n).

不幸的是,我不得不承认我不太明白如何定义一个作为 InputIterator 模型的 EdgeIterator。
这是我到目前为止取得的成功:

template< class EdgeIterator, class Edge >
class MyEdgeIterator// : public input_iterator< std::pair<int, int> > 
{
public:
        MyEdgeIterator() {}; 

        MyEdgeIterator(EdgeIterator& rhs) : actual_edge_it_(rhs) {}; 

        MyEdgeIterator(const MyEdgeIterator& to_copy) {}; 

        bool operator==(const MyEdgeIterator& to_compare)
        {   
                return actual_edge_it_ == to_compare.actual_edge_it_;
        }   

        bool operator!=(const MyEdgeIterator& to_compare)
        {   
                return !(*this == to_compare);
        }   

        Edge operator*() const
        {   
                return *actual_edge_it_;
        }

        const MyEdgeIterator* operator->() const;
        MyEdgeIterator& operator ++()
        {   
                ++actual_edge_it_;
                return *this;
        }   

        MyEdgeIterator operator ++(int)
        {   
                MyEdgeIterator<EdgeIterator, Edge> tmp = *this;
                ++*this;
                return tmp;
        }

private:
        EdgeIterator& actual_edge_it_;
}

然而,这并没有像预期的那样工作,我已经没有线索了。
那么,如何定义合适的 InputIterator?

最佳答案

我遇到了同样的问题,我用了Transform Iterator从 boost 。

我实现了进行转换的 copyGraph。

例子:

SimpleGraphType simple = copyGraph<ComplexGraphType, SimpleGraphType>(complex);

还有代码:

#include <boost/iterator/transform_iterator.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <utility>

template <class Graph>
class EdgeToPair
{
public:
   typedef std::pair<int, int> result_type;

   typedef typename boost::graph_traits<Graph>::edge_iterator edge_iter;
   typedef typename std::iterator_traits<edge_iter>::value_type edge_type;

   EdgeToPair(const Graph& graph) : _graph(graph) {}

   std::pair<int, int> operator()(edge_type edge) const
   {
      return std::make_pair(boost::source(edge, _graph),
                            boost::target(edge, _graph));
   }

private:
   const Graph& _graph;
};

template <class GraphIn, class GraphOut>
GraphOut copyGraph(const GraphIn& graphIn)
{
   EdgeToPair<GraphIn> edgeToPair(graphIn);

   typename boost::graph_traits<GraphIn>::edge_iterator ei, eend;

   boost::tie(ei, eend) = boost::edges(graphIn);

   return GraphOut(boost::make_transform_iterator(ei, edgeToPair),
                   boost::make_transform_iterator(eend, edgeToPair),
                   boost::num_vertices(graphIn));
}

我确信使用 boost::copy_graph 存在更好的解决方案并通过自定义 edge_copy 但至少这个解决方案有效。

关于c++ - Boost 图的自定义 InputIterator (BGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2855957/

相关文章:

c++ - vector<shared_ptr<T>> 和 vector<shared_ptr<const T>> 之间的高效转换

c++ - boost 线程卡在 _endthreadex

c++ - 在 Xcode 6 中运行 cpp-netlib - 编译但在运行时崩溃

java - 如何迭代以对象作为键并将 <objects> 设置为值的 HashMap 并打印两者?

javascript - d3.behavior.zoom() 将图形移动到 Angular 落而不是中心

c++ - 如何在 std::map 类中定义迭代器

c++ - 调用基类转换为派生类的函数

c++ - 如何将 boost asio 接受器限制为本地主机和/或本地网络?

algorithm - 3-SAT如何简化为Independent set?

c++ - Boost::mutex 性能对比 pthread_mutex_t