我想使用的add_edge功能时找出顶点创作的行为。 下面是一个例子:
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
using namespace boost;
typedef adjacency_list<> Graph;
typedef graph_traits<Graph>::vertex_iterator v_iter;
Graph g;
add_edge(1,2,g);
add_edge(1,4,g);
add_edge(2,3,g);
add_edge(2,6,g);
std::cout << "num edges: " << num_edges(g) << "; num vertices: " << num_vertices(g) << std::endl;
for (std::pair<v_iter,v_iter> vp = vertices(g); vp.first != vp.second; vp.first++) {
std::cout << *vp.first << " ";
}
收益:
bash-3.2$ ./main
num edges: 4; num vertices: 7
0 1 2 3 4 5 6
为什么要创建这些顶点? 该图总共具有1,2,3,4和6为顶点,5不7.看来,函数创建从0到顶点的最高值的顶点。
我真不“知道什么怎么回事,所以任何帮助是极大的赞赏。
预先感谢您非常多。
对于每个顶点邻接表店相邻节点:
按照文档:
VertexList
该选择器用于用来表示图的顶点列表的容器。
默认值: 血管内皮细胞
这意味着该索引到载体是顶点ID。 你不能有一个包含索引1的向量,但没有指标0。因此,你会得到所有的中间指标“免费”。
当然,你可以调整这一点:使用如listS
为顶点一览表看到它住在Coliru
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <algorithm>
using namespace boost;
typedef adjacency_list<boost::listS> Graph;
typedef graph_traits<Graph>::vertex_iterator v_iter;
int main()
{
Graph g;
graph_traits<Graph>::vertex_descriptor v[] = {
{}, // unused
add_vertex(g), add_vertex(g), add_vertex(g),
add_vertex(g), add_vertex(g), add_vertex(g),
};
add_edge(v[1], v[2], g);
add_edge(v[1], v[4], g);
add_edge(v[2], v[3], g);
add_edge(v[2], v[6], g);
std::cout << "num edges: " << num_edges(g) << "; num vertices: " << num_vertices(g) << std::endl;
for (std::pair<v_iter, v_iter> vp = vertices(g); vp.first != vp.second; ++vp.first) {
std::cout << std::distance(v, std::find(std::begin(v), std::end(v), *vp.first)) << " ";
}
}
打印
num edges: 4; num vertices: 6
1 2 3 4 5 6 Press any key to continue . . .
只要想通了:该值将被视为指标。 例如:
add_edge(1,200);
假定实际上有200点的顶点,并连接与201顶点的第二个。 显然,将自动创建它们之间的所有顶点。
BW