I have no idea about how to fix this problem. Is there anyone can give me some help? Please have a look at the following code, and then tell me what is the problem. It could be deep copy and shallow copy problem but I am not sure.
It is not a hard thing but I have lost most of my memory for C++ ...so I really need help for this issue. It will be appreciated that anyone can tell why I cannot get the size of the children, in the second level of the nodes.
I have been tortured by this prob for days.
Thanks
It is not a hard thing but I have lost most of my memory for C++ ...so I really need help for this issue. It will be appreciated that anyone can tell why I cannot get the size of the children, in the second level of the nodes.
I have been tortured by this prob for days.
Thanks
Code:
#include <iostream>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <utility>
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::map;
using std::set;
using std::pair;
using std::make_pair;
class Test {
public:
map<string, Test> children;
string name;
// constructor
Test();
Test(const Test& node);
void insert();
};
Test::Test(){
name = "testing";
}
Test::Test(const Test& node) {
children = node.children;
name = node.name;
}
void Test::insert() {
Test node;
Test node2;
Test node3;
name = "Rico";
string name = "piggie";
string childName1="car";
string childName2="house";
children.insert(make_pair(name,node));
node.children.insert(make_pair(childName1,node2));
node.children.insert(make_pair(childName2,node3));
}
int main(int argc, const char* argv[]){
Test rico;
rico.insert();
cout << rico.name << endl;
cout << rico.children.size() << endl;
string piggie_str = "piggie";
if(rico.children.find(piggie_str) != rico.children.end()){
map<string,Test>::const_iterator iter = rico.children.find(piggie_str);
Test tempNode = (Test)iter->second;
cout << "child name:" << iter->first << endl;
cout << "child's children size:" << tempNode.children.size() << endl;
}
}