Home > Articles > Programming > C/C++

  • Print
  • + Share This
Like this article? We recommend

Manipulating Property Values

In order to access the properties of your graph, you need to obtain a helper object from the Graph library. You obtain one object for each property type. The objects are template objects, and you can obtain them after you’ve created an instance of your Graph type. Here are the lines that obtain objects for the three properties I declared in the preceding section:

property_map<Graph, vertex_name_t>::type
  city_name = get(vertex_name, g);
property_map<Graph, vertex_index2_t>::type
  city_index2 = get(vertex_index2, g);
property_map<Graph, edge_weight_t>::type
  edge_distance = get(edge_weight, g);

Don’t worry that the function name is the somewhat generic word get. The function is well-defined as a typed template, so it will compile just fine.

The first statement creates an object for obtaining the vertex_name property. Look closely at the parameters. On the left, the parameters to the template are the Graph type defined earlier, along with the tag for the property, vertex_name_t. On the right, inside the call to get, is not the tag name, but rather an enumeration type associated with the property, vertex_name. Each predefined tag name also includes an enumeration for each type. The Graph object itself, g, is the second parameter to the get function.

Remember, these three calls each obtain an object that you can use for getting properties for a vertex or edge. Although the sample code pieces I’ve been giving you so far haven’t created any vertices, here’s a sample line I’ll use later on to obtain a property:

std::cout << city_name[*vp.first];

where vp.first is a pointer to a vertex. The object, city_name, works like a map, and it takes a vertex object as a parameter. It then returns the appropriate property—in this case, the city name.

  • + Share This
  • 🔖 Save To Your Account