我正在尝试使用这个Boost C ++代码,我遇到了问题

时间:2011-02-09 01:44:46

标签: c++ templates boost

首先我会解释,然后我会粘贴代码。我实际上复制了这个例子中的代码 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

1 个答案:

答案 0 :(得分:2)

它应该是“edge_array”而不是“edge”(那个是原始代码的一部分)。