簡體   English   中英

boost圖庫示例不編譯

[英]boost graph library example does not compile

我需要在圖表上進行拓撲排序。 Boost圖庫可以做到這一點。 但是,我在這個網站上找到的例子不能編譯。 錯誤是名稱空間提升中的“沒有成員命名”topological_sort“。導致此錯誤的原因是什么?

#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>

#include "boost/graph/graph_traits.hpp"
#include "boost/graph/adjacency_list.hpp"

using namespace boost;

int main(int argc, char *argv[])
{
  typedef adjacency_list<vecS, vecS, undirectedS> UndirectedGraph;

  //Our set of edges, which basically are just converted into ints (0-4)
  enum {A, B, C, D, E, N};
  const char *name = "ABCDE";

  //An edge is just a connection between two vertitices. Our verticies above
  //are an enum, and are just used as integers, so our edges just become
  //a std::pair<int, int>
  typedef std::pair<int, int> Edge;

  //Example uses an array, but we can easily use another container type
  //to hold our edges.
  std::vector<Edge> edgeVec;
  edgeVec.push_back(Edge(A,B));
  edgeVec.push_back(Edge(A,D));
  edgeVec.push_back(Edge(C,A));
  edgeVec.push_back(Edge(D,C));
  edgeVec.push_back(Edge(C,E));
  edgeVec.push_back(Edge(B,D));
  edgeVec.push_back(Edge(D,E));

  //Now we can initialize our graph using iterators from our above vector
  UndirectedGraph g(edgeVec.begin(), edgeVec.end(), N);

  std::cout << num_edges(g) << "\n";

  //Ok, we want to see that all our edges are now contained in the graph
  typedef graph_traits<UndirectedGraph>::edge_iterator edge_iterator;

  //Tried to make this section more clear, instead of using tie, keeping all
  //the original types so it's more clear what is going on
  std::pair<edge_iterator, edge_iterator> ei = edges(g);
  for(edge_iterator edge_iter = ei.first; edge_iter != ei.second; ++edge_iter) {
      std::cout << "(" << source(*edge_iter, g) << ", " << target(*edge_iter, g) << ")\n";
  }

  std::cout << "\n";
  //Want to add another edge between (A,E)?
  add_edge(A, E, g);

  //Print out the edge list again to see that it has been added
  for(edge_iterator edge_iter = ei.first; edge_iter != ei.second; ++edge_iter) {
      std::cout << "(" << source(*edge_iter, g) << ", " << target(*edge_iter, g) << ")\n";
  }

  //Finally lets add a new vertex - remember the verticies are just of type int
  int F = add_vertex(g);
  std::cout << F << "\n";

  //Connect our new vertex with an edge to A...
  add_edge(A, F, g);

  //...and print out our edge set once more to see that it was added
  for(edge_iterator edge_iter = ei.first; edge_iter != ei.second; ++edge_iter) {
      std::cout << "(" << source(*edge_iter, g) << ", " << target(*edge_iter, g) << ")\n";
  }

  std::deque<int> topo_order;
  boost::topological_sort(g, std::front_inserter(topo_order));
  /*
  for(std::deque<int>::const_iterator i = topo_order.begin();
      i != topo_order.end();
      ++i)
  {
      cout << index(*i) << endl;
  }
  */

  for(auto i: topo_order)
  {
      std::cout << i << std::endl;
  }
  return 0;
}

我已將代碼復制並粘貼到qtcreator空項目文件中。 我的配置使用c ++ 11。

出錯的原因是你錯過了boost庫中topological_sort.hpp的包含。

#include "boost/graph/topological_sort.hpp"

這個最小的例子(你問題中的代碼的精簡版)成功編譯:

#include <deque>

#include "boost/graph/graph_traits.hpp"
#include "boost/graph/adjacency_list.hpp"
#include "boost/graph/topological_sort.hpp"

using namespace boost;

int main(int argc, char *argv[])
{
  typedef adjacency_list<vecS, vecS, undirectedS> UndirectedGraph;
  UndirectedGraph g;
  std::deque<int> topo_order;
  boost::topological_sort(g, std::front_inserter(topo_order));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM