我正在尝试使用这个 Boost C++代码,我有一个问题
首先我会解释,然后我将粘贴代码。我实际上复制了这个例子中的代码 http://www.boost.org/doc /libs/1_45_0/libs/graph/example/prim-example.cpp 然后我尝试让它处理文本文件的输入,就像我对 Boost Kruskal 算法所做的那样。
使用调试器,我知道该函数的第二个参数想要“结束”边数组。这就是我通过函数调用给出的内容,我不明白。
Graph g(edges, edge_array + num_edges,weights, num_nodes);
我收到此错误
1>c:\users\edmond\documents\visual studio 2008\projects\boost prim algo\boost prim algo\main.cpp(61) : error C2661: 'boost::adjacency_list<OutEdgeListS,VertexListS,DirectedS,VertexProperty,EdgeProperty>::adjacency_list' : no overloaded function takes 4 arguments
1> with
1> [
1> OutEdgeListS=boost::vecS,
1> VertexListS=boost::vecS,
1> DirectedS=boost::undirectedS,
1> VertexProperty=boost::property<boost::vertex_distance_t,int>,
1> EdgeProperty=boost::property<boost::edge_weight_t,int>
1> ]
这是完整的代码。我已经注释了原始代码,但是你也可以在我给出的网站上找到原始代码。
//=======================================================================
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include <boost/config.hpp>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>
int
main()
{
using namespace boost;
typedef adjacency_list < vecS, vecS, undirectedS,
property<vertex_distance_t, int>, property < edge_weight_t, int > > Graph;
typedef std::pair < int, int >E;
//const int num_nodes = 5;
//E edges[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3),
// E(3, 4), E(4, 0)
//};
//int weights[] = { 1, 1, 2, 7, 3, 1, 1 };
//int num_edges = 7;
//Lire un fichier contenant les 2 structures de données
int num_nodes = 0;
std::size_t num_edges = 0;
int * weights;
E * edge_array;
static char ligne[50]; //Ligne lue
bool premiereLignefaite = false;
FILE* fichier = fopen("graph_poids.txt", "r");
int i = 0;
while (fgets(ligne, 50, fichier) != NULL) //retourne 0 quand on a end-of-file
{
//La premiere ligne est différente
if (premiereLignefaite == false) {
//Initialiser une matrice d'adjacence NxN
sscanf(ligne, "%d %d", &num_nodes, &num_edges );
edge_array = new E[num_edges];
weights = new int[num_edges];
premiereLignefaite = true;
continue;
}
//On construit notre liste d'arêtes
int sommet1, sommet2, poids;
sscanf(ligne, "%d %d %d", &sommet1, &sommet2, &poids);
weights[i] = poids;
edge_array[i].first = sommet1;
edge_array[i].second = sommet2;
i++;
}
E* machin = edge_array + num_edges; //aller au dernier élément
Graph g(edges, edge_array + num_edges, weights, num_nodes);
//Graph g(edges, edges + sizeof(edges) / sizeof(E), weights, num_nodes);
property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, g);
std::vector < graph_traits < Graph >::vertex_descriptor >
p(num_vertices(g));
prim_minimum_spanning_tree(g, &p[0]);
for (std::size_t i = 0; i != p.size(); ++i)
if (p[i] != i)
std::cout << "parent[" << i << "] = " << p[i] << std::endl;
else
std::cout << "parent[" << i << "] = no parent" << std::endl;
return EXIT_SUCCESS;
}
如果您想尝试的话,这里有一些测试数据。文件的名称是 graph_poids.txt
14 19
0 2 4
0 4 9
0 1 7
1 6 2
2 3 6
3 5 7
3 4 4
4 13 9
5 7 7
5 6 6
6 8 9
6 10 4
7 9 4
7 8 7
8 11 7
8 10 7
9 12 7
9 11 10
12 13 5
First I'll explain, then I'll paste the code. I actually copied the code from this example
http://www.boost.org/doc/libs/1_45_0/libs/graph/example/prim-example.cpp
And then I'm trying to get it to work with an input from a text file, just like I did for the Boost Kruskal algorithm.
Using the debugger, I know that the second argument of this function wants the "end" the array of edges. That's what I'm giving it with my function call, i don't understand.
Graph g(edges, edge_array + num_edges, weights, num_nodes);
I get this error
1>c:\users\edmond\documents\visual studio 2008\projects\boost prim algo\boost prim algo\main.cpp(61) : error C2661: 'boost::adjacency_list<OutEdgeListS,VertexListS,DirectedS,VertexProperty,EdgeProperty>::adjacency_list' : no overloaded function takes 4 arguments
1> with
1> [
1> OutEdgeListS=boost::vecS,
1> VertexListS=boost::vecS,
1> DirectedS=boost::undirectedS,
1> VertexProperty=boost::property<boost::vertex_distance_t,int>,
1> EdgeProperty=boost::property<boost::edge_weight_t,int>
1> ]
Here is the full code. I have commented the original code, but you can also find the original code on the website I gave.
//=======================================================================
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include <boost/config.hpp>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>
int
main()
{
using namespace boost;
typedef adjacency_list < vecS, vecS, undirectedS,
property<vertex_distance_t, int>, property < edge_weight_t, int > > Graph;
typedef std::pair < int, int >E;
//const int num_nodes = 5;
//E edges[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3),
// E(3, 4), E(4, 0)
//};
//int weights[] = { 1, 1, 2, 7, 3, 1, 1 };
//int num_edges = 7;
//Lire un fichier contenant les 2 structures de données
int num_nodes = 0;
std::size_t num_edges = 0;
int * weights;
E * edge_array;
static char ligne[50]; //Ligne lue
bool premiereLignefaite = false;
FILE* fichier = fopen("graph_poids.txt", "r");
int i = 0;
while (fgets(ligne, 50, fichier) != NULL) //retourne 0 quand on a end-of-file
{
//La premiere ligne est différente
if (premiereLignefaite == false) {
//Initialiser une matrice d'adjacence NxN
sscanf(ligne, "%d %d", &num_nodes, &num_edges );
edge_array = new E[num_edges];
weights = new int[num_edges];
premiereLignefaite = true;
continue;
}
//On construit notre liste d'arêtes
int sommet1, sommet2, poids;
sscanf(ligne, "%d %d %d", &sommet1, &sommet2, &poids);
weights[i] = poids;
edge_array[i].first = sommet1;
edge_array[i].second = sommet2;
i++;
}
E* machin = edge_array + num_edges; //aller au dernier élément
Graph g(edges, edge_array + num_edges, weights, num_nodes);
//Graph g(edges, edges + sizeof(edges) / sizeof(E), weights, num_nodes);
property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, g);
std::vector < graph_traits < Graph >::vertex_descriptor >
p(num_vertices(g));
prim_minimum_spanning_tree(g, &p[0]);
for (std::size_t i = 0; i != p.size(); ++i)
if (p[i] != i)
std::cout << "parent[" << i << "] = " << p[i] << std::endl;
else
std::cout << "parent[" << i << "] = no parent" << std::endl;
return EXIT_SUCCESS;
}
Here is some test data if you want to try it. The name of the file is graph_poids.txt
14 19
0 2 4
0 4 9
0 1 7
1 6 2
2 3 6
3 5 7
3 4 4
4 13 9
5 7 7
5 6 6
6 8 9
6 10 4
7 9 4
7 8 7
8 11 7
8 10 7
9 12 7
9 11 10
12 13 5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它应该是“edge_array”而不是“edges”(它是原始代码的一部分)。
It should be "edge_array" and not "edges" (that one was part of the original code).