fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. struct Node
  6. {
  7. Node(const std::string& name, int weight) :
  8. name_(name),
  9. weight_(weight)
  10. {}
  11. std::string name_;
  12. int weight_;
  13. };
  14.  
  15. bool operator<(const Node& lhs, const Node& rhs){
  16. return lhs.name_ < rhs.name_;
  17. }
  18.  
  19. using Graph = std::map < Node, std::map< Node, int>>;
  20.  
  21. int main()
  22. {
  23. Graph graph;
  24.  
  25. Node A("A", 3);
  26. Node B("B", 1);
  27. Node C("C", 4);
  28. graph[A][B] = 5;
  29. graph[A][C] = 1;
  30. graph[B][C] = 7;
  31.  
  32. std::cout << graph[A][C]<<'\n'; //inja weight edge(A,C) yani 1 chap mishe
  33.  
  34. //Chap ras haye motasel be A
  35. auto& connected= graph[A];
  36. for (auto& edge_weight : connected){
  37. std::cout << "Edge : (" << A.name_ << "," <<
  38. edge_weight.first.name_ << ") Weight : " <<
  39. edge_weight.second << '\n';
  40. }
  41.  
  42. }
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
1
Edge : (A,B) Weight : 5
Edge : (A,C) Weight : 1