c++ - boost::copy_graph 的vertex_copy 是如何工作的?

标签 c++ boost boost-graph

我正在使用 boost::copy_graph复制 adjacency_list进入另一个adjacency_list用不同的VertexProperties模板。为此,我尝试使用 vertex_copy参数(doc here)。我遇到了一个编译器错误,它告诉我第二个(待复制的)图的顶点属性类型错误。

最小示例

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/copy.hpp>

typedef boost::adjacency_list<boost::vecS,
                              boost::vecS,
                              boost::undirectedS,
                              uint32_t,
                              float> AdjacencyList;

typedef AdjacencyList::vertex_descriptor VertexID;

struct custom_property
{
    uint32_t label;
    float f;
};

typedef boost::adjacency_list<boost::vecS,
                              boost::vecS,
                              boost::undirectedS,
                              custom_property,
                              float> AdjacencyListCustom;

struct vertex_copier
{
    void operator() (uint32_t &input, custom_property &output)
    {
        output.label = input;
        output.f = 0.;
    }
};


int main(int argc, char** argv)
{
    AdjacencyList adj;
    VertexID id_0 = boost::add_vertex(0, adj);
    VertexID id_1 = boost::add_vertex(1, adj);
    VertexID id_2 = boost::add_vertex(2, adj);
    VertexID id_3 = boost::add_vertex(4, adj);
    boost::add_edge(id_0, id_1, 1.0f, adj);
    boost::add_edge(id_2, id_3, 2.0f, adj);

    AdjacencyListCustom adj_custom;
    boost::copy_graph(adj, adj_custom,  boost::vertex_copy(vertex_copier()));

}

g++编译错误

...
/usr/include/boost/graph/copy.hpp:164:22: error: no match for call to '(vertex_copier) (boost::iterators::detail::iterator_facade_base<boost::range_detail::integer_iterator<long unsigned int>, long unsigned int, boost::iterators::random_access_traversal_tag, long unsigned int, long int, false, false>::reference, boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, custom_property, float> >::vertex_descriptor&)'
           copy_vertex(*vi, new_v);
           ~~~~~~~~~~~^~~~~~~~~~~~
/path/to/file.cpp: note: candidate: void vertex_copier::operator()(uint32_t&, custom_property&)
     void operator() (uint32_t &input, custom_property &output)
          ^~~~~~~~
/path/to/file.cpp: note:   no known conversion for argument 2 from 'boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, custom_property, float> >::vertex_descriptor {aka long unsigned int}' to 'custom_property&'

这告诉我 vertex_copy实际上是试图复制 vertex_descriptor ,这是一个 long unsigned int , 不是 custom_property .这似乎违背了文档中的说明:

这是一个二元函数,可将原始图中顶点的属性复制到拷贝中的相应顶点。

如何vertex_copy工作?它可以用于在复制的图中设置/定义原始图中不存在的顶点属性吗?如果不是,是否必须在复制后通过遍历图形来设置这些属性?我可以整体应用映射还是必须访问和更新每个顶点?

编辑: 如果我尝试使用 copy_graph未指定 vertex_copy ,然后出现错误,因为 = custom_property 之间不存在运算符和 uint32_t .

最佳答案

暂时回避这个问题,最简单的实现方法是向自定义属性添加适当的转换构造函数:

struct custom_property {
    custom_property(uint32_t label = 0, float f = 0) : label(label), f(f) {}
    uint32_t label;
    float f;
};

在这种情况下,一个简单的拷贝就可以了:

boost::copy_graph(adj, adj_custom);

查看 Live On Coliru


关于顶点复制器,它接收顶点描述符。要访问顶点属性,您需要有图形引用:

struct vertex_copier {
    AdjacencyList& from;
    AdjacencyListCustom& to;

    void operator()(AdjacencyList::vertex_descriptor input, AdjacencyListCustom::vertex_descriptor output) const {
        to[output] = { from[input], 0.f };
    }
};

在这种情况下你会调用东西:

boost::copy_graph(adj, adj_custom, boost::vertex_copy(vertex_copier{adj, adj_custom}));

同样, Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/copy.hpp>
#include <iostream>

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, uint32_t, float> AdjacencyList;

typedef AdjacencyList::vertex_descriptor VertexID;

    struct custom_property {
        //custom_property(uint32_t label = 0, float f = 0) : label(label), f(f) {}
        uint32_t label;
        float f;
    };

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, custom_property, float> AdjacencyListCustom;

struct vertex_copier {
    AdjacencyList& from;
    AdjacencyListCustom& to;

    void operator()(AdjacencyList::vertex_descriptor input, AdjacencyListCustom::vertex_descriptor output) const {
        to[output] = { from[input], 0.f };
    }
};

int main(int argc, char **argv) {
    AdjacencyList adj;
    VertexID id_0 = boost::add_vertex(0, adj);
    VertexID id_1 = boost::add_vertex(1, adj);
    VertexID id_2 = boost::add_vertex(2, adj);
    VertexID id_3 = boost::add_vertex(4, adj);
    boost::add_edge(id_0, id_1, 1.0f, adj);
    boost::add_edge(id_2, id_3, 2.0f, adj);

    AdjacencyListCustom adj_custom;
    boost::copy_graph(adj, adj_custom, boost::vertex_copy(vertex_copier{adj, adj_custom}));
}

关于c++ - boost::copy_graph 的vertex_copy 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40442721/

相关文章:

c++ - std::ostream 接口(interface)到 OLE IStream

c++ - 基于 boost::asio 的慢速 http 客户端 - (分块传输)

c++ - 将 adjacency_list 复制到不同的 VertexList 和 EdgeList 模板

c++ - 加速人员检测程序,使用 OpenCV 3.0 编写

c++ - 套接字连接上的缓冲区溢出

C++ 多线程错误

c++ - 为什么没有 boost::intrusive::map?

c++ - vector 模板冲突声明

c++ - BGL 中的 slistS 发生了什么?

c++ - 是否可以将boost库的广度优先搜索算法应用于矩阵?