default-constructor.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #include <boost/config.hpp>
  9. #include <fstream>
  10. #include <boost/graph/adjacency_list.hpp>
  11. using namespace boost;
  12. template < typename Graph > void
  13. read_graph_file(std::istream & in, Graph & g)
  14. {
  15. typedef typename graph_traits < Graph >::vertices_size_type size_type;
  16. size_type n_vertices;
  17. in >> n_vertices; // read in number of vertices
  18. for (size_type i = 0; i < n_vertices; ++i) // Add n vertices to the graph
  19. add_vertex(g);
  20. size_type u, v;
  21. while (in >> u) // Read in pairs of integers as edges
  22. if (in >> v)
  23. add_edge(u, v, g);
  24. else
  25. break;
  26. }
  27. int
  28. main(int argc, const char** argv)
  29. {
  30. typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list
  31. vecS, // Store vertex set in a std::vector
  32. directedS // The graph is directed
  33. > graph_type;
  34. graph_type g; // use default constructor to create empty graph
  35. std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
  36. read_graph_file(file_in, g);
  37. assert(num_vertices(g) == 15);
  38. assert(num_edges(g) == 19);
  39. return 0;
  40. }