OK, now I've got the other idea and the other code. But I don't know 1) where I've got memory outflow (what kind of situation can cause this memory outflow), 2) how to avoid it/ improve the code
Thanks for help, greetings!
Structure which represents directory - directory, obviously, has got its name and subdirectories (and constructor is to simplify the code).
Code:
struct Directory {
std::string name;
std::vector<Directory*> children;
Directory(std::string name_) : name(name_) { };
};
The simplest recurrence function shows the tree. (std::for_each is in <algorithm>):
Code:
void print(Directory *dir) {
std::cout << dir->name << " : " << std::endl;
std::for_each(dir->children.begin(), dir->children.end(), print);
}
This code shows the tree in the better way.
Code:
int nesting = 0;
void print(Directory *dir) {
for (int i = 0; i < nesting; ++i) std::cout << ' ';
std::cout << dir->name << " => (" << std::endl;
++nesting;
std::for_each(dir->children.begin(), dir->children.end(), print);
--nesting;
for (int i = 0; i < nesting; ++i) std::cout << ' ';
std::cout << ")" << std::endl;
}
And exemplary tree:
Code:
int main(void) {
/*
- root
- sub1
- sub1.1
- sub2
- sub3
- sub3.1
- sub3.1.1
*/
Directory *root = new Directory("root");
Directory *sub1 = new Directory("sub1");
Directory *sub11 = new Directory("sub1.1");
Directory *sub2 = new Directory("sub2");
Directory *sub3 = new Directory("sub3");
Directory *sub31 = new Directory("sub3.1");
Directory *sub311 = new Directory("sub3.1.1");
root->children.push_back(sub1);
sub1->children.push_back(sub11);
root->children.push_back(sub2);
root->children.push_back(sub3);
sub3->children.push_back(sub31);
sub31->children.push_back(sub311);
print(root);
return 0;
}