Boost图形库:named_graph和remove_vertex
boost graph library: named_graph and remove_vertex
我正在尝试使用named_graph mixin,我有点困惑remove_vertex()应该如何工作。
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/graph/adjacency_list.hpp>
struct vertex_info {
std::string name; // uses vertex_from_name<vertex_info>
vertex_info(const std::string &name_) : name(name_) { }
};
ostream& operator<<(ostream & os, const vertex_info &v)
{
os << v.name;
return os;
}
namespace boost { namespace graph {
template<typename Type>
struct vertex_name_extractor
{
typedef Type type;
typedef const std::string& result_type;
result_type operator()(const Type& v) const
{
return v.name;
}
};
template<>
struct internal_vertex_name<vertex_info>
{
typedef vertex_name_extractor<vertex_info> type;
};
template<>
struct internal_vertex_constructor<vertex_info>
{
typedef vertex_from_name<vertex_info> type;
};
} }
typedef adjacency_list< vecS, vecS, undirectedS, vertex_info, edge_info> graph_t;
namespace bg=boost::graph;
int main()
{
using namespace std;
graph_t g;
int i;
typedef graph_traits<graph_t>::vertex_descriptor vert;
for(i=0;i < 10;++i)
{
string t_name("Vertex");
vert V;
t_name += lexical_cast<string>(i);
V = add_vertex(t_name,g);
}
typedef graph_t::vertex_name_type name_t;
name_t s_temp("Vertex2");
optional<vert> V(
find_vertex(s_temp,g));
if( V ) {
cout << "Found vertex:" << *V << 'n';
//remove_vertex(*V,g); // (1)
//remove_vertex(vertex(*V,g),g); // (2)
//remove_vertex(g[*V],g); // (3)
//remove_vertex(s_temp,g); // (4)
} else {
cout << "Vertex not foundn";
}
graph_traits<graph_t>::vertex_iterator v_i, v_end;
for(tie(v_i,v_end) = vertices(g); v_i != v_end; ++v_i)
{
cout << ''' << g[*v_i] << ''' << endl;;
}
}
当我尝试使用(3)或(4)时,我得到一个错误,因为没有匹配的函数调用' remove_vertex(vertex_info&, graph_t&) '
adjacency_list.hpp:2211 candidate: remove_vertex(typename graph_t::vertex_descriptor, graph_t &)
但是当我尝试使用(1)或(2)时,我得到了从' long unsigned int '到' const char* '的无效转换的错误。
error: initializing argument 1 of ‘std::basic_string<...'
boost/graph/named_graph.hpp:349
template<BGL_NAMED_GRAPH_PARAMS>
inline void BGL_NAMED_GRAPH::removing_vertex(Vertex vertex)
{
named_vertices.erase(vertex); //line 349
}
vertex_name提取器的result_type应该删除const和引用限定符。函数对象应该指定它返回一个const引用。这允许适当的元函数依赖于result_type而不删除它们。可以更容易地指定重载函数。
template<typename Type>
struct vertex_name_extractor
{
typedef Type type;
typedef std::string result_type;
const result_type& operator()(const Type& v) const
{
return v.name;
}
} ;
如果我们指定自己的构造函数,创建绑定的VertexProperty可以更容易地扩展。
template<typename VertexProperty>
struct vertex_info_constructor
{
typedef VertexProperty return_type;
typedef typename vertex_name_extractor<VertexProperty>::result_type argument_type;
return_type operator()(argument_type n)
{
VertexProperty v(n);
return v;
}
};
template<>
struct internal_vertex_constructor<vertex_info>
{
typedef vertex_info_constructor<vertex_info> type;
};
adjacency_list使用MI mixin,其基类为maybe_named_graph<>。在internal_vertex_name::类型为void时关闭named_graphs的示例之后,我添加了的部分专门化如下所示:
template<typename Graph, typename Vertex>
struct maybe_named_graph<Graph, Vertex, vertex_info, vertex_name_extractor<vertex_info> >
: public named_graph<Graph, Vertex, vertex_info>
{
typedef named_graph<Graph, Vertex, vertex_info> Base;
//maybe_named_graph() { }
typedef typename detail::extract_bundled_vertex<vertex_info>::type
bundled_vertex_property_type;
void added_vertex(Vertex v) { Base::added_vertex(v); }
void removing_vertex(Vertex v) {
const std::string &name = extract_name((Base::derived()[v]));
Base::named_vertices.erase(name);
}
void clearing_graph() { Base::clearing_graph(); }
optional<Vertex>
vertex_by_property(const bundled_vertex_property_type& t)
{
return Base::vertex_by_property(t);
}
};
现在邻接ency_list的remove_vertex(VertexDescriptor, Graph)调用我的专用removing_node,它通过顶点描述符从named_graph中移除vertex_name。
注意:vecS使用了,所以你仍然要小心迭代器无效。
相关文章:
- 使用函数"remove"删除重复元素
- 链表的泛型函数remove()与成员函数remove)
- Qt:remove() 和 rmdir() 有什么区别
- remove(str.begin(), str.end(), );无法正常工作(我正在使用视觉工作室 2012)
- 提升图库示例 凯文培根的六度:示例中的 Vertex() 是什么
- std::remove() 按预期处理文字,但不能与取消引用的迭代器一起工作
- 图形"Vertex cover"暴力算法
- 从 MATLAB 到 C++:相当于带有选项 'remove' 的 bwmorph
- 为二进制树编码remove()函数
- OpenGL,Cubemap,Vertex和FragmentShader
- 如何理解"most modern linkers will remove redundant code like template instantiations."
- 使用容器中元素的别名删除带有 std::list::remove 的元素是否正确?
- C++代码"x.erase(std::remove(x.begin(), x.end(), ), x.end())"是如何工作的?
- std::remove, std::move(range) and moved-from elements
- 为什么我需要转换算法 remove() 的参数
- 在不同类型的向量上使用 std::remove 的不同结果
- 为什么 VERTEX 着色器中的矩阵乘法无法正常工作
- 在不存在的记录上调用 remove 会使程序崩溃
- 以变量作为参数的 remove()
- 尝试在 Kdevelop中调试时如何修复"WebConnect: Starting server failed. Try to remove stale socket."?