Posted By

jimfred on 11/13/08


Tagged

template c tree mfc


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

khouser


core::tree is an STL-like template to implement, for example, a tree of CStrings


 / Published in: C++
 

URL: http://www.gamedev.net/reference/programming/features/coretree2/

Tree.h can be found here: http://www.gamedev.net/reference/programming/features/coretree2/tree.h

  1. // TreeTemplateTest.cpp : console application to test/demo core::tree
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "tree.h"
  6. #include <iostream>
  7.  
  8. typedef core::tree<CString> CTreeStr;
  9. typedef core::tree<CString>::iterator CTreeStrRef;
  10.  
  11. void CoutTree( CTreeStrRef Arg, int levelArg )
  12. {
  13. if ( levelArg==0 )
  14. {
  15. std::wcout << L"Root: " << (LPCTSTR)Arg.data() << std::endl;
  16. }
  17.  
  18. levelArg++;
  19.  
  20. for ( CTreeStrRef x = Arg.begin(); x != Arg.end(); ++x)
  21. {
  22. std::wcout << levelArg << L", " << (LPCTSTR)x.data() << std::endl;
  23. CoutTree( x, levelArg );
  24. }
  25. }
  26.  
  27. int _tmain(int argc, _TCHAR* argv[])
  28. {
  29. CTreeStr tree;
  30. CTreeStr::iterator ref;
  31. CTreeStrRef ref2;
  32.  
  33. *tree = L"a";
  34. ref = tree.insert( L"bb" );
  35. ref.insert( L"ddd" );
  36. ref = ref.insert( L"eee" );
  37. ref.insert( L"hhhh" );
  38.  
  39. ref = tree.insert( L"cc" );
  40. ref.insert( L"fff" );
  41. ref = ref.insert( L"ggg" );
  42. ref.insert( L"iiii" );
  43.  
  44. CoutTree( tree, 0 );
  45.  
  46. return 0;
  47. }
  48.  
  49. /* Output
  50. Root: a
  51. 1, bb
  52. 2, ddd
  53. 2, eee
  54. 3, hhhh
  55. 1, cc
  56. 2, fff
  57. 2, ggg
  58. 3, iiii
  59. */

Report this snippet  

You need to login to post a comment.