Difference between revisions of "Boost/BGL/EdgeProperties"
From ProgrammingExamples
Daviddoria (Talk | contribs) (Created page with '==CreateGraph.cpp== <source lang="cpp"> #include <iostream> // for std::cout #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> /…') |
Daviddoria (Talk | contribs) |
||
Line 1: | Line 1: | ||
==CreateGraph.cpp== | ==CreateGraph.cpp== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | #include <iostream> | + | #include <iostream> |
#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::edge_weight_t, double> EdgeWeightProperty; | typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty; | ||
− | // | + | /* |
− | typedef boost::adjacency_list<boost::vecS, boost::vecS, boost:: | + | adjacency_list<OutEdgeList, VertexList, Directed, |
+ | VertexProperties, EdgeProperties, | ||
+ | GraphProperties, EdgeList> | ||
+ | */ | ||
+ | typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeightProperty> Graph; | ||
int main(int,char*[]) | int main(int,char*[]) | ||
{ | { | ||
− | // | + | // Create a graph object |
− | Graph g( | + | Graph g(3); |
− | + | EdgeWeightProperty e1 = 5; | |
− | EdgeWeightProperty | + | add_edge(0, 1, e1, g); |
− | add_edge( | + | |
+ | EdgeWeightProperty e2 = 3; | ||
+ | add_edge(1, 2, e2, g); | ||
− | |||
boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), g); | boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), g); | ||
− | + | ||
typedef boost::graph_traits<Graph>::edge_iterator edge_iter; | typedef boost::graph_traits<Graph>::edge_iterator edge_iter; | ||
std::pair<edge_iter, edge_iter> edgePair; | std::pair<edge_iter, edge_iter> edgePair; | ||
for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first) | for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first) | ||
{ | { | ||
− | std::cout | + | std::cout << EdgeWeightMap[*edgePair.first] << " "; |
− | + | ||
} | } | ||
− | + | return 0; | |
} | } | ||
Revision as of 10:56, 27 January 2011
CreateGraph.cpp
#include <iostream> #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty; /* adjacency_list<OutEdgeList, VertexList, Directed, VertexProperties, EdgeProperties, GraphProperties, EdgeList> */ typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeightProperty> Graph; int main(int,char*[]) { // Create a graph object Graph g(3); EdgeWeightProperty e1 = 5; add_edge(0, 1, e1, g); EdgeWeightProperty e2 = 3; add_edge(1, 2, e2, g); boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), g); typedef boost::graph_traits<Graph>::edge_iterator edge_iter; std::pair<edge_iter, edge_iter> edgePair; for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first) { std::cout << EdgeWeightMap[*edgePair.first] << " "; } return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) Project(Properties) set(Boost_USE_MULTITHREADED ON) # which is the default FIND_PACKAGE(Boost 1.38 COMPONENTS program_options required) INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS}) LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS}) ADD_EXECUTABLE(Properties Properties.cpp) target_link_libraries(Properties)