Difference between revisions of "Boost/BGL/VertexProperties"

From ProgrammingExamples
< Boost‎ | BGL
Jump to: navigation, search
(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:…')
 
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
<source lang="cpp">
 
<source lang="cpp">
 
#include <iostream>
 
#include <iostream>
#include <boost/graph/graph_traits.hpp>
 
 
#include <boost/graph/adjacency_list.hpp>
 
#include <boost/graph/adjacency_list.hpp>
  
typedef boost::property<boost::vertex_property_tag, double> VertexProperty;
+
struct VertexProperty
 +
{
 +
  int Id;
 +
};
  
/*
+
// Define the type of the graph - this specifies a bundled property for vertices
adjacency_list<OutEdgeList, VertexList, Directed,
+
              VertexProperties, EdgeProperties,
+
              GraphProperties, EdgeList>
+
*/
+
 
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperty> Graph;
 
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperty> Graph;
  
 
int main(int,char*[])
 
int main(int,char*[])
 
{
 
{
   // Create a graph object
+
   std::cout << "Method 1:" << std::endl;
  Graph g(3);
+
 
    
 
    
   boost::property_map<Graph, VertexProperty>::type value = get(VertexProperty(), g);
+
   // Create a graph object
 +
  Graph g;
 +
  Graph::vertex_descriptor v0 = boost::add_vertex(g);
 +
  Graph::vertex_descriptor v1 = boost::add_vertex(g);
  
   boost::put(value, 0, 1.2);
+
   g[v0].Id = 21;
   boost::put(value, 1, 2.3);
+
   g[v1].Id = 34;
  boost::put(value, 2, 3.4);
+
  
 +
  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 << g[*vertexPair.first].Id <<  std::endl;
 +
    }
  
 +
  std::cout << "Method 2:" << std::endl;
 +
 
 +
  {
 +
  // Create a graph object
 +
  Graph g(2);
 +
 
 +
  // Assign "21" as the Id of the 0th vertex, and "34" as the Id of the 1st vertex
 +
  typedef boost::graph_traits<Graph>::vertex_iterator VItr;
 +
  VItr vitr, vend;
 +
  boost::tie( vitr, vend) = boost::vertices(g);
 +
  g[*vitr].Id = 21;
 +
  vitr++;
 +
  g[*vitr].Id = 34;
 +
 +
  boost::tie( vitr, vend) = boost::vertices(g);
 +
  for ( ; vitr != vend ; ++vitr )
 +
    std::cout << g[*vitr].Id << "\n";
 +
  }
 +
 +
 
 
   return 0;
 
   return 0;
 
}
 
}
Line 44: Line 69:
  
 
ADD_EXECUTABLE(VertexProperties VertexProperties.cpp)
 
ADD_EXECUTABLE(VertexProperties VertexProperties.cpp)
 
 
  
 
</source>
 
</source>

Latest revision as of 08:49, 16 November 2011

VertexProperties.cpp

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
 
struct VertexProperty
{
  int Id;
};
 
// Define the type of the graph - this specifies a bundled property for vertices
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperty> Graph;
 
int main(int,char*[])
{
  std::cout << "Method 1:" << std::endl;
 
  // Create a graph object
  Graph g;
  Graph::vertex_descriptor v0 = boost::add_vertex(g);
  Graph::vertex_descriptor v1 = boost::add_vertex(g);
 
  g[v0].Id = 21;
  g[v1].Id = 34;
 
  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 << g[*vertexPair.first].Id <<  std::endl;
    }
 
  std::cout << "Method 2:" << std::endl;
 
  {
  // Create a graph object
  Graph g(2);
 
  // Assign "21" as the Id of the 0th vertex, and "34" as the Id of the 1st vertex
  typedef boost::graph_traits<Graph>::vertex_iterator VItr;
  VItr vitr, vend;
  boost::tie( vitr, vend) = boost::vertices(g);
  g[*vitr].Id = 21;
  vitr++;
  g[*vitr].Id = 34;
 
  boost::tie( vitr, vend) = boost::vertices(g);
  for ( ; vitr != vend ; ++vitr )
    std::cout << g[*vitr].Id << "\n";
  }
 
 
  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)