fork download
  1. #include <boost/graph/adjacency_list.hpp>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. struct SLCompBlock {
  6. std::string name;
  7. // Constructor for convenience
  8. SLCompBlock(const std::string& n) : name(n) {}
  9. };
  10.  
  11. int main() {
  12. typedef boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, const SLCompBlock*> Graph;
  13. Graph boostGraph;
  14.  
  15. // Example SLCompBlock objects
  16. SLCompBlock block1("Block 1");
  17. SLCompBlock block2("Block 2");
  18. SLCompBlock block3("Block 3");
  19.  
  20. // Add vertices to the graph
  21. auto v1 = boost::add_vertex(&block1, boostGraph);
  22. auto v2 = boost::add_vertex(&block2, boostGraph);
  23. auto v3 = boost::add_vertex(&block3, boostGraph);
  24.  
  25. // Add some edges for demonstration purposes
  26. boost::add_edge(v1, v2, boostGraph);
  27. boost::add_edge(v2, v3, boostGraph);
  28.  
  29. // Loop over all edges and print information about the connected nodes
  30. typedef boost::graph_traits<Graph>::edge_iterator edge_iter;
  31. std::pair<edge_iter, edge_iter> ei;
  32. for (ei = boost::edges(boostGraph); ei.first != ei.second; ++ei.first) {
  33. // Get the current edge
  34. auto edge = *ei.first;
  35.  
  36. // Get the source and target vertices of the current edge
  37. auto u = boost::source(edge, boostGraph);
  38. auto v = boost::target(edge, boostGraph);
  39.  
  40. // Access the SLCompBlock objects
  41. const SLCompBlock* sourceBlock = boostGraph[u];
  42. const SLCompBlock* targetBlock = boostGraph[v];
  43.  
  44. // Print the connection
  45. std::cout << "Edge connects \"" << sourceBlock->name << "\" to \"" << targetBlock->name << "\"" << std::endl;
  46. }
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Edge connects "Block 1" to "Block 2"
Edge connects "Block 2" to "Block 3"