1: //test_graph2.cpp 3: #include <iostream> 4: #include <string> 5: using namespace std; 7: #include <boost/graph/adjacency_list.hpp> 8: using namespace boost; 10: int main() 11: { 12: //Property types 13: typedef property<edge_weight_t, int> EdgeWeightProperty; 14: typedef property<vertex_name_t, std::string, 15: property<vertex_index2_t, int> > VertexProperties; 17: //Graph type 18: typedef adjacency_list<vecS, vecS, undirectedS, 19: VertexProperties, EdgeWeightProperty> Graph; 21: //Graph instance 22: Graph g; 24: //Property accessors 25: property_map<Graph, vertex_name_t>::type 26: city_name = get(vertex_name, g); 27: property_map<Graph, vertex_index2_t>::type 28: city_index2 = get(vertex_index2, g); 29: property_map<Graph, edge_weight_t>::type 30: edge_distance = get(edge_weight, g); 32: //Create the vertices 33: typedef graph_traits<Graph>::vertex_descriptor Vertex; 34: Vertex u1; 35: u1 = add_vertex(g); 36: city_name[u1] = "Los Angeles"; 37: city_index2[u1] = 3; 39: Vertex u2; 40: u2 = add_vertex(g); 41: city_name[u2] = "Bakersfield"; 42: city_index2[u2] = 2; 44: Vertex u3; 45: u3 = add_vertex(g); 46: city_name[u3] = "New York"; 47: city_index2[u3] = 1; 49: //Create the edges 50: typedef graph_traits<Graph>::edge_descriptor Edge; 51: Edge e1; 52: e1 = (add_edge(u1, u2, g)).first; 53: edge_distance[e1] = 100; 55: Edge e2; 56: e2 = add_edge(u1, u3, g).first; 57: edge_distance[e2] = 2500; 59: //Iterate through the vertices and print them out 60: typedef graph_traits<Graph>::vertex_iterator vertex_iter; 61: std::pair<vertex_iter, vertex_iter> vp; 62: for (vp = vertices(g); vp.first != vp.second; ++vp.first) 63: std::cout << city_name[*vp.first] 64: << " " << city_index2[*vp.first] << std::endl; 65: std::cout << std::endl; 67: //Iterate through the edges and print them out 68: typedef graph_traits<Graph>::edge_iterator edge_iter; 69: std::pair<edge_iter, edge_iter> ep; 70: edge_iter ei, ei_end; 71: for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) 72: std::cout << edge_distance[*ei] << endl; 73: }