fork(2) download
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using std::cout;
  7.  
  8. template< typename T >
  9. struct tree_node
  10. {
  11. T t;
  12. std::vector<tree_node> children;
  13. void walk_depth_first() const;
  14. };
  15.  
  16. template< typename T >
  17. void tree_node<T>::walk_depth_first() const
  18. {
  19. cout<<t<<"\n";;
  20. for ( auto & n: children ) n.walk_depth_first();
  21. }
  22.  
  23. int main()
  24. {
  25. tree_node<std::string> tree;
  26. tree.t="base";
  27. tree.children.push_back({"a"});
  28. tree.children.push_back({"b"});
  29. tree.children.push_back({"c"});
  30. tree.children.at(1).children.push_back({"ba"});
  31. tree.walk_depth_first();
  32. }
  33.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
base
a
b
ba
c