adjacency_list_io.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // (C) Copyright Francois Faure 2001
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/config.hpp>
  6. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  7. #error adjacency_list_io.hpp has not been ported to work with VC++
  8. #endif
  9. #include <boost/graph/adjacency_list_io.hpp>
  10. #include <fstream>
  11. using namespace boost;
  12. //======== my data structure
  13. struct MyStruct { double value; };
  14. std::ostream& operator << ( std::ostream& out, const MyStruct& s )
  15. {
  16. out << s.value << " ";
  17. return out;
  18. }
  19. std::istream& operator >> ( std::istream& in, MyStruct& s )
  20. {
  21. in >> s.value;
  22. return in;
  23. }
  24. //======== vertex properties
  25. struct n1_t { enum { num = 23063}; typedef vertex_property_tag kind; };
  26. struct n2_t { enum { num = 23062}; typedef vertex_property_tag kind; };
  27. struct n3_t { enum { num = 23061}; typedef vertex_property_tag kind; };
  28. typedef property< n1_t, int,
  29. property< n2_t, double,
  30. property< n3_t, MyStruct > > > VertexProperty;
  31. //====== edge properties
  32. struct e1_t { enum { num = 23064}; typedef edge_property_tag kind; };
  33. typedef property<e1_t, double> EdgeProperty;
  34. //===== graph types
  35. typedef
  36. adjacency_list<vecS, listS, directedS, no_property, no_property>
  37. Graph1;
  38. typedef
  39. adjacency_list<setS, setS, bidirectionalS, VertexProperty, EdgeProperty>
  40. Graph2;
  41. int
  42. main()
  43. {
  44. // read Graph1
  45. Graph1 g1;
  46. std::ifstream readFile1("data1.txt");
  47. readFile1 >> read( g1 );
  48. std::cout << "graph g1 from file data1.txt:\n"
  49. << write( g1 )
  50. << std::endl;
  51. // read Graph2 and all internal properties
  52. Graph2 g2;
  53. std::ifstream readFile2("data2.txt");
  54. readFile2 >> read( g2 );
  55. std::cout << "graph g2 from file data2.txt:\n"
  56. << write( g2 )
  57. << std::endl;
  58. // read Graph2, no property given. Write no property.
  59. Graph2 g21;
  60. std::ifstream readFile21("data1.txt");
  61. readFile21 >> read( g21, no_property(), no_property() );
  62. std::cout << "graph g21 from file data1.txt:\n"
  63. << write(g21, no_property(), no_property())
  64. << std::endl;
  65. // read Graph2, incomplete data in a different order. Write it diffently.
  66. Graph2 g31;
  67. std::ifstream readFile31("data3.txt");
  68. typedef property< n3_t, MyStruct, property< n1_t, int > > readNodeProp;
  69. readFile31 >> read( g31, readNodeProp() , EdgeProperty() );
  70. std::cout << "graph g31 from file data3.txt:\n"
  71. << write( g31, property<n3_t, MyStruct>(), EdgeProperty() )
  72. << std::endl;
  73. return 0;
  74. }