Difference between revisions of "Boost/BGL/VertexProperties"
From ProgrammingExamples
Daviddoria (Talk | contribs) (Created page with '==VertexProperties.cpp== <source lang="cpp"> #include <iostream> #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> typedef boost::property<boost:…') |
Daviddoria (Talk | contribs) |
||
Line 2: | Line 2: | ||
<source lang="cpp"> | <source lang="cpp"> | ||
#include <iostream> | #include <iostream> | ||
+ | #include <string> | ||
#include <boost/graph/graph_traits.hpp> | #include <boost/graph/graph_traits.hpp> | ||
#include <boost/graph/adjacency_list.hpp> | #include <boost/graph/adjacency_list.hpp> | ||
− | typedef boost::property<boost:: | + | typedef boost::property<boost::vertex_name_t, std::string> VertexProperty; |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperty> Graph; | typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperty> Graph; | ||
Line 18: | Line 13: | ||
// Create a graph object | // Create a graph object | ||
Graph g(3); | Graph g(3); | ||
+ | |||
+ | boost::property_map<Graph, boost::vertex_name_t>::type value = boost::get(boost::vertex_name_t(), g); | ||
+ | boost::put(value, 0, "Vertex0"); | ||
+ | boost::put(value, 1, "Vertex1"); | ||
+ | boost::put(value, 2, "Vertex2"); | ||
+ | |||
+ | typedef boost::property_map<Graph, boost::vertex_index_t>::type IndexMap; | ||
+ | IndexMap index = get(boost::vertex_index, g); | ||
+ | |||
+ | typedef boost::graph_traits<Graph>::vertex_iterator vertex_iter; | ||
+ | std::pair<vertex_iter, vertex_iter> vertexPair; | ||
+ | for (vertexPair = vertices(g); vertexPair.first != vertexPair.second; ++vertexPair.first) | ||
+ | { | ||
+ | std::cout << index[*vertexPair.first] << " : " << value[*vertexPair.first] << std::endl; | ||
+ | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
return 0; | return 0; | ||
} | } |
Revision as of 14:30, 27 January 2011
VertexProperties.cpp
#include <iostream> #include <string> #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> typedef boost::property<boost::vertex_name_t, std::string> VertexProperty; typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperty> Graph; int main(int,char*[]) { // Create a graph object Graph g(3); boost::property_map<Graph, boost::vertex_name_t>::type value = boost::get(boost::vertex_name_t(), g); boost::put(value, 0, "Vertex0"); boost::put(value, 1, "Vertex1"); boost::put(value, 2, "Vertex2"); typedef boost::property_map<Graph, boost::vertex_index_t>::type IndexMap; IndexMap index = get(boost::vertex_index, g); typedef boost::graph_traits<Graph>::vertex_iterator vertex_iter; std::pair<vertex_iter, vertex_iter> vertexPair; for (vertexPair = vertices(g); vertexPair.first != vertexPair.second; ++vertexPair.first) { std::cout << index[*vertexPair.first] << " : " << value[*vertexPair.first] << std::endl; } return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) Project(VertexProperties) set(Boost_USE_MULTITHREADED ON) FIND_PACKAGE(Boost 1.38 COMPONENTS required) INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS}) LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS}) ADD_EXECUTABLE(VertexProperties VertexProperties.cpp)