boost::copy_graph 的vertex_copy是如何工作的?
How does boost::copy_graph's vertex_copy work?
我使用boost::copy_graph
将adjacency_list
复制到具有不同VertexProperties
模板的另一个adjacency_list
中。要做到这一点,我试图使用vertex_copy
参数(文档在这里)。我遇到了一个编译器错误,告诉我有一个错误的类型为第二个(待复制)图的顶点属性。
#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()));
}
c++编译错误
...
/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);
看生活在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}));
, 生活在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}));
}
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::filesystem::copy throws filesystem_error
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- 一个工作的std::copy示例——打印一个数组
- C++ "copy"导致异常,而暴力破解工作正常
- C++在使用运算符=()时使用copy ctor-这正是如何工作的
- std::copy的分段错误,memcpy 工作