0

i was trying to build a tree out of a map, to show a class Hierachy with base classes and derrived classes. One node can have multiple children, and sub-children

Here my code:

std::map<std::string, std::string> myClassMap; // 1. String (key) = Classname, 2. String = BaseClass


myClassMap.insert ( std::pair<std::string, std::string>("DerivedClass1", "OverallBaseClass"));
myClassMap.insert ( std::pair<std::string, std::string>("DerivedClass2", "OverallBaseClass"));
myClassMap.insert ( std::pair<std::string, std::string>("DerivedClass3", "OverallBaseClass") );
myClassMap.insert ( std::pair<std::string, std::string>("DerivedClass2-1'","DerivedClass2") );
myClassMap.insert ( std::pair<std::string, std::string>("DerivedClass2-1-1","DerivedClass2-1") );
myClassMap.insert ( std::pair<std::string, std::string>("DerivedClass2-1-2","DerivedClass2-1") );
myClassMap.insert ( std::pair<std::string, std::string>("DerivedClass2-2","DerivedClass2") );


  //and so on

I have a method:

void addTreeChild(QTreeWidgetItem *parent, std::string keyClassName)
{

    QTreeWidgetItem *treeItem = new QTreeWidgetItem();
    treeItem->setText(0, QString(keyClassName));
    parent->addChild(treeItem);

}

which i can call and add the children to the tree. Now my question is, how can i iterate trough the map, and add the children to my tree, so it arise a Class-Hierarchy

user3126813
  • 57
  • 1
  • 7
  • Seems like you can use this: http://stackoverflow.com/questions/4844886/how-to-loop-through-a-c-map – River Jun 15 '15 at 17:56
  • 1
    I am unclear. Do you have `addTreeChildren` that adds a child, or do you not? If you already have it, why do you need help "add the children to my tree"? And, what are you doing when iterating through the map? In short, what is actually your problem? – Yakk - Adam Nevraumont Jun 15 '15 at 18:18
  • Sorry, i updatet my question. I do have a the method, but in only adds children under children, without the Hierarchy (DerrivedClass1/2/3 inherits from OverallBaseClass)-->Short, how can i add the Items to the tree with the right hierachical structure? – user3126813 Jun 16 '15 at 06:13

1 Answers1

0

First of all remember that strings need double quotes - not single.

You probably want a multimap instead of a map as you are using the same key several times (which can't be done in a map).

You can iterate like this:

for (auto i : myClassMap)
{
    // i.first now holds the first string (key)
    // i.second now holds the second string (value)
}

So something like:

    std::multimap<std::string, std::string> myClassMap;
    myClassMap.insert ( std::pair<std::string, std::string>("OverallBaseClass", "DerivedClass1"));
    myClassMap.insert ( std::pair<std::string, std::string>("OverallBaseClass", "DerivedClass2"));
    myClassMap.insert ( std::pair<std::string, std::string>("DerivedClass2", "DerivedClass3") );
    for (auto i : myClassMap)
    {
        cout << i.first << "," << i.second << endl;
    }

will produce:

DerivedClass2,DerivedClass3

OverallBaseClass,DerivedClass1

OverallBaseClass,DerivedClass2

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63