Difference between revisions of "Boost/BGL/BundledProperties"
From ProgrammingExamples
Daviddoria (Talk | contribs) (Created page with '==BundledProperties.cpp== <source lang="cpp"> #include <iostream> #include <boost/graph/adjacency_list.hpp> // Create a struct to hold several properties struct MyProperty { i…') |
Daviddoria (Talk | contribs) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
#include <boost/graph/adjacency_list.hpp> | #include <boost/graph/adjacency_list.hpp> | ||
− | // Create a struct to hold | + | // Create a struct to hold properties for each vertex |
− | struct | + | struct VertexProperties |
{ | { | ||
− | int | + | int VertexIntProperty; |
− | std::string | + | std::string VertexStringProperty; |
}; | }; | ||
− | // | + | // Create a struct to hold properties for each edge |
− | + | struct EdgeProperties | |
+ | { | ||
+ | int EdgeIntProperty; | ||
+ | std::string EdgeStringProperty; | ||
+ | }; | ||
− | // Define the type of the graph | + | // Define the type of the graph |
− | + | typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties> Graph; | |
int main(int,char*[]) | int main(int,char*[]) | ||
{ | { | ||
// Create a graph object | // Create a graph object | ||
− | Graph g | + | Graph g; |
− | + | // Add two vertices | |
− | + | Graph::vertex_descriptor v0 = boost::add_vertex(g); | |
− | + | Graph::vertex_descriptor v1 = boost::add_vertex(g); | |
− | + | // Add an edge | |
− | + | std::pair<Graph::edge_descriptor, bool> e01 = boost::add_edge(v0, v1, g); | |
− | + | ||
− | + | // Set and output the properties of a single vertex | |
− | + | g[v0].VertexIntProperty = 5; | |
+ | g[v0].VertexStringProperty = "mystring"; | ||
+ | std::cout << g[v0].VertexIntProperty << " : " << g[v0].VertexStringProperty << std::endl; | ||
+ | |||
+ | // Set and output the properties of a single edge | ||
+ | g[e01.first].EdgeIntProperty = 5; | ||
+ | g[e01.first].EdgeStringProperty = "mystring"; | ||
+ | std::cout << g[e01.first].EdgeIntProperty << " : " << g[e01.first].EdgeStringProperty << std::endl; | ||
+ | |||
+ | // Set and output the properties of each vertex | ||
+ | std::pair<Graph::vertex_iterator, Graph::vertex_iterator> vertexIteratorRange = boost::vertices(g); | ||
+ | for(Graph::vertex_iterator vertexIterator = vertexIteratorRange.first; vertexIterator != vertexIteratorRange.second; ++vertexIterator) | ||
+ | { | ||
+ | g[*vertexIterator].VertexIntProperty = 5; | ||
+ | g[*vertexIterator].VertexStringProperty = "test"; | ||
+ | std::cout << g[*vertexIterator].VertexIntProperty << " : " << g[*vertexIterator].VertexStringProperty << std::endl; | ||
+ | } | ||
− | + | // Set and output the properties of each edge | |
− | std::pair< | + | std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g); |
− | for ( | + | for(Graph::edge_iterator edgeIterator = edgeIteratorRange.first; edgeIterator != edgeIteratorRange.second; ++edgeIterator) |
{ | { | ||
− | std::cout << | + | g[*edgeIterator].EdgeIntProperty = 5; |
+ | g[*edgeIterator].EdgeStringProperty = "test"; | ||
+ | std::cout << g[*edgeIterator].EdgeIntProperty << " : " << g[*edgeIterator].EdgeStringProperty << std::endl; | ||
} | } | ||
Latest revision as of 08:50, 16 November 2011
BundledProperties.cpp
#include <iostream> #include <boost/graph/adjacency_list.hpp> // Create a struct to hold properties for each vertex struct VertexProperties { int VertexIntProperty; std::string VertexStringProperty; }; // Create a struct to hold properties for each edge struct EdgeProperties { int EdgeIntProperty; std::string EdgeStringProperty; }; // Define the type of the graph typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties> Graph; int main(int,char*[]) { // Create a graph object Graph g; // Add two vertices Graph::vertex_descriptor v0 = boost::add_vertex(g); Graph::vertex_descriptor v1 = boost::add_vertex(g); // Add an edge std::pair<Graph::edge_descriptor, bool> e01 = boost::add_edge(v0, v1, g); // Set and output the properties of a single vertex g[v0].VertexIntProperty = 5; g[v0].VertexStringProperty = "mystring"; std::cout << g[v0].VertexIntProperty << " : " << g[v0].VertexStringProperty << std::endl; // Set and output the properties of a single edge g[e01.first].EdgeIntProperty = 5; g[e01.first].EdgeStringProperty = "mystring"; std::cout << g[e01.first].EdgeIntProperty << " : " << g[e01.first].EdgeStringProperty << std::endl; // Set and output the properties of each vertex std::pair<Graph::vertex_iterator, Graph::vertex_iterator> vertexIteratorRange = boost::vertices(g); for(Graph::vertex_iterator vertexIterator = vertexIteratorRange.first; vertexIterator != vertexIteratorRange.second; ++vertexIterator) { g[*vertexIterator].VertexIntProperty = 5; g[*vertexIterator].VertexStringProperty = "test"; std::cout << g[*vertexIterator].VertexIntProperty << " : " << g[*vertexIterator].VertexStringProperty << std::endl; } // Set and output the properties of each edge std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g); for(Graph::edge_iterator edgeIterator = edgeIteratorRange.first; edgeIterator != edgeIteratorRange.second; ++edgeIterator) { g[*edgeIterator].EdgeIntProperty = 5; g[*edgeIterator].EdgeStringProperty = "test"; std::cout << g[*edgeIterator].EdgeIntProperty << " : " << g[*edgeIterator].EdgeStringProperty << std::endl; } return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) Project(BundledProperties) 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(BundledProperties BundledProperties.cpp) target_link_libraries(BundledProperties)