fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class node
  6. {
  7. public:
  8. vector<node*> next;
  9. void add_arc(node & a);
  10.  
  11. string somestring;
  12.  
  13. };
  14.  
  15. void node::add_arc(node & a)
  16. {
  17. node *b = &a;
  18. next.push_back(b); //only copyies nodes
  19. }
  20.  
  21. int main() {
  22. vector<node> nodes;
  23. node a;
  24. node b;
  25. node c;
  26.  
  27. a.somestring = "a";
  28. b.somestring = "b";
  29. c.somestring = "c";
  30.  
  31. a.add_arc(b); //a should point to b
  32. a.add_arc(c); //a should point to c
  33.  
  34. nodes.push_back(a);
  35. nodes.push_back(b);
  36. nodes.push_back(c);
  37.  
  38. cout << nodes[0].next.size() << endl; // prints "2", works fine
  39. cout << nodes[0].next[0]->somestring << endl; //empty
  40. return 0;
  41. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
2
b