Hello! It is my first post and I've got probably easy question. I can use containers like list, vector. But I think there is no tree container in standard containers of C++. I found this site http://www.gamedev.net/reference/art...rticle2192.asp and I'd like to use it. The problem is that I do not know how to create trees with this tree.h.
This is my listing:
I'd like to create an examplary tree (so that I can train how to use tree.h):Code:#include <cstdlib> #include <iostream> #include <string> //source of tree.h: //http://www.gamedev.net/reference/articles/article2192.asp #include "tree.h" using namespace std; class dir //directory { public: dir() : itsName("katalog") {} //default constructor explicit dir(const string &name) : itsName(name) {} //constructor const string &getName() const { return itsName; } //accessor bool operator==(const dir &rhs) const { return this->getName() == rhs.getName(); } bool operator<(const dir &rhs) const { return this->getName() < rhs.getName(); } private: string itsName; }; void temp() //temporary function which creates simple tree of directories //and shows the result { using namespace core; //namespace from tree.h //typedef tree<dir> treeDir; tree<dir> leafDir; //it creates tree of dir tree<dir>::iterator iter = leafDir.insert(dir("main")); //root of tree iter = iter.insert(dir("dir1")); //... AND HERE I CAN'T WRITE THE PROPER CODE //////////////////////////////////////////////////// // output the dir tree //////////////////////////////////////////////////// for (tree<dir>::const_iterator iter = leafDir.begin(); iter != leafDir.end(); ++iter) { std::cout << iter.data().getName() << std::endl; tree<dir>::const_iterator inner = iter; // dir's iterators are containers themselves - use the // same iterator to traverse inwards through the dir for (inner = inner.in(); inner != inner.end(); inner = inner.in()) { for (int tabs = 1; tabs < inner.level(); ++tabs) std::cout << "\t"; std::cout << (*inner).getName() << std::endl; } } } int main(int argc, char *argv[]) { temp(); system("PAUSE"); return EXIT_SUCCESS; }
But I don't know how to create new "levels" of the tree, how to "step back" into lower level, how to create branches/leaves on the same level - please, help me.main
---dir1
------subdir1
---------subdir4
------subdir2
---dir2
------subdir3
---dir3
Greetings and thanks in advance for your help!



LinkBack URL
About LinkBacks


