如何从boost图列表容器中查看顶点/边
How to view vertices/edges from boost graph list containers
我试图使用listS
作为顶点/边缘容器,以便我可以安全地删除边缘。然而,我遇到了一个问题-我不知道如何看到我的顶点或边!当我尝试遍历它们并打印出来时,由于某种原因,它只打印出它们的地址。
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_iterator.hpp>
#include <utility>
using namespace std;
typedef boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef boost::graph_traits <Graph>::edge_iterator edgeIt;
typedef boost::graph_traits<Graph>::vertex_iterator vertexIt;
typedef map<vertex_t,size_t> IndexMap;
int main(int argc, const char * argv[]) {
Graph g;
IndexMap mapIndex;
vertex_t v0 = boost::add_vertex(g);
vertex_t v1 = boost::add_vertex(g);
vertex_t v2 = boost::add_vertex(g);
vertex_t v3 = boost::add_vertex(g);
mapIndex[v0] = 0;
mapIndex[v1] = 1;
mapIndex[v2] = 2;
mapIndex[v3] = 3;
boost::add_edge(v0,v2,g);
boost::add_edge(v1,v3,g);
boost::add_edge(v1,v0,g);
for(pair<vertexIt,vertexIt> vi = boost::vertices(g); vi.first != vi.second; ++vi.first) {
cout << *vi.first << endl;
}
for(pair<edgeIt,edgeIt> ei = boost::edges(g); ei.first != ei.second; ++ei.first) {
cout << source(*ei.first, g) << " -> " << target(*ei.first, g) << endl;
cout << *ei.first << endl;
}
}
输出为:
0x7f9409404b10
0x7f9409404b70
0x7f9409404c00
0x7f9409404c40
0x7f9409404b10 -> 0x7f9409404c00
(0x7f9409404b10,0x7f9409404c00)
0x7f9409404b70 -> 0x7f9409404c40
(0x7f9409404b70,0x7f9409404c40)
0x7f9409404b70 -> 0x7f9409404b10
(0x7f9409404b70,0x7f9409404b10)
如果我使用vecS
而不是listS
,它工作得很好,但然后我在删除顶点时遇到麻烦。那么我如何使用listS
查看边/顶点?
编辑:解决方案
解决方案1:
使用结构体存储属性:
struct vertex {
int id;
}
typedef boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, vertex> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef boost::graph_traits <Graph>::edge_iterator edgeIt;
typedef boost::graph_traits<Graph>::vertex_iterator vertexIt;
typedef map<vertex_t,size_t> IndexMap;
//MAKE YOUR GRAPH
...
//
//Assign vertex IDs
int currentID = 0;
for(pair<vertexIt,vertexIt> it = boost::vertices(g); it.first != it.second; ++it.first) {
g[*it.first].id = currentID++;
}
//Access them when displaying edges:
for(pair<vertexIt,vertexIt> it = boost::vertices(g); it.first != it.second; ++it.first) {
cout << g[*it.first].id << endl;
}
//Access them when displaying vertices:
for(pair<edgeIt,edgeIt> it = boost::edges(g); it.first != it.second; ++it.first) {
cout << g[source(*it.first,g)].id << " " << g[target(*it.first,g)].id << endl;
}
解决方案2:
使用属性映射(继续我的第一个代码)
for(pair<vertexIt,vertexIt> vi = boost::vertices(g); vi.first != vi.second; ++vi.first) {
cout << mapIndex[*vi.first] << endl;
}
for(IndexMap::iterator it = mapIndex.begin(); it != mapIndex.end(); ++it) {
cout << it->first << ": " << it->second << endl;
}
Boost中的顶点和边数据类型抽象为descriptors
。它们或多或少是指针。
当使用vecS
时,描述符为size_t
索引,其对应的顶点和边索引映射默认可用。如果你想用listS
为你的边和顶点创建一个整数索引,你必须自己创建。
我认为像boost那样做的一个好方法是这样的。
typedef std::map<vertex_descriptor,size_t> StdVertexIndexMap;
StdVertexIndexMap viMap;
typedef boost::associative_property_map<StdVertexIndexMap> VertexIndexMap;
VertexIndexMap v_index(viMap);
vertex_iterator vi,ve;
size_t i = 0;
for(boost::tie(vi,ve) = boost::vertices(g); vi != ve; ++vi){
boost::put(v_index,*vi,i);
++i;
}
同样适用于边。参照我之前的问题。将其包装在boost::associative_property_map
或类似的东西中是Boost方式,因此使用boost::get
和boost::put
的算法将正常工作。
如果您不打算在boost算法中使用它,您可以使用简单的std::map
。
(推测)一种可能性是你正在返回指针的值,即地址。
尝试使用两次解引用操作符**
来测试这些地址上的内容,并查看返回值是否合理。
相关文章:
- Boost Read_graphml 无法正确读取 xml,它给出了所有顶点,但它们是空的
- 使用Vertex_descriptor boost图在顶点上打印或迭代
- 如何在不重新索引顶点的情况下调用"boost::remove_vertex"?
- Boost:列出GraphMl中指定的顶点和边缘属性
- 在Boost Graph库中选择给定顶点的随机进出邻居的有效方法
- 如何使用boost::graph dijkstra的算法,如果顶点属性是指针?
- 使用Boost :: Graph随机访问顶点
- C Boost ::如何从顶点描述符创建地图
- 在BOOST图中找到给定2个顶点的多条边
- Boost:如何移除顶点的所有外边缘
- 通过允许用户选择顶点数量,使用Boost库生成图形
- Boost图库undirected_Graph:如何指定顶点类型(例如int)
- 从 boost::adjacency_list 获取边属性(包括相关顶点)
- 使用BOOST复制具有相邻顶点的边及其属性
- 使用BOOST复制边及其顶点和特性
- 从Boost Graph中删除多个顶点
- Boost有向图:比较顶点的边
- 为什么Boost图库在删除顶点时会使所有迭代器无效
- Boost:: Dijkstra 最短路径,如何从路径迭代器获取顶点索引
- 如何使用boost::graph算法与listS,setS作为顶点/边缘容器