Tim Sloane's Profile User Rating: -----

Reputation: 1 Apprentice
Group:
New Members
Active Posts:
13 (0.02 per day)
Joined:
27-August 12
Profile Views:
1,761
Last Active:
User is offline Oct 22 2012 11:16 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   Tim Sloane has not set their status

Posts I've Made

  1. In Topic: Store all combinations of children in a vector of structs

    Posted 25 Sep 2012

    OK, I see what you're doing. Thanks a lot! You've given me a lot of good ideas.
  2. In Topic: Store all combinations of children in a vector of structs

    Posted 24 Sep 2012

    Hello again baavgai!

    I started to implement your method, but I'm not sure I understand your statement about making a vector of instances and looping. Don't I have to instantiate each child class individually (even if it is a vector of the parent class), in which case I can just call "process" after each instantiation, making the vector pointless? More so if I make "process" part of the constructor...
  3. In Topic: Store all combinations of children in a vector of structs

    Posted 21 Sep 2012

    Ah hah! That is a much more O-O way of doing this, thanks! Inheritance and polymorphism are the half of O-O in which I've never developed my skills very well. I really appreciate your help.

    By the way, my daughters saw me reading this thread and asked me what I was talking with Hagrid about. When I told them Hagrid was helping me with C++ programming, they were very surprised. :wink:
  4. In Topic: Store all combinations of children in a vector of structs

    Posted 20 Sep 2012

    I ended up using callbacks to generalize the code. This is basically what it looks like now. This particular code is untested, as my actual implementation is a bit different. Just a warning if you try to copypasta and compile :wink: :

    void XMLToStructStep (xml_node parentNode, vector<xmlCombination> &xmlCombo1, vector<xmlCombination> &xmlCombo2, void (*funcPt) (vector<xmlCombination>::iterator iter, xml_node &chNode) ) {
        setup2.clear();
        for (vector<xmlCombination>::iterator iter = xmlCombo1.begin(); iter < xmlCombo1.end(); iter++) {
            for (xml_node chNode =  parentNode.first_child() ; chNode; chNode = chNode.next_sibling() ) {
                funcPt (iter, chNode); 
                xmlCombo2.push_back(*soIter);
            }
        }
    }
    
    main() {
        vector<xmlCombination> vo_xmlCombo, vo_xmlComboTemp;
        xml_node rootNode = xml_File.child("root"); //code to load the XML file into xml_File not shown here
    
        class xmlGetFuncs { //callback functions for each XML element in the setup file
        public:
            static void string1Get (vector<xmlCombination>::iterator soIter, xml_node &chNode) { soIter->str1 = chNode.text().get(); }
            static void int1Get (vector<xmlCombination>::iterator soIter, xml_node &chNode)    { soIter->int1 = chNode.text().as_int(); }  
            static void string2Get (vector<xmlCombination>::iterator soIter, xml_node &chNode) { soIter->str2 = chNode.text().get(); }
            static void double1Get (vector<xmlCombination>::iterator soIter, xml_node &chNode) { soIter->dbl = chNode.text().as_double(); }
        };
    
        vo_xmlCombo.push_back(xmlCombination());
        XMLToStructStep(rootNode.child("string1"), vo_xmlCombo, vo_xmlComboTemp, &(xmlGetFuncs::string1Get) );
        XMLToStructStep(rootNode.child("int1"), vo_xmlComboTemp, vo_xmlCombo, &(xmlGetFuncs::int1Get) );
        XMLToStructStep(rootNode.child("string2"), vo_xmlCombo, vo_xmlComboTemp, &(xmlGetFuncs::string2Get) );
        XMLToStructStep(rootNode.child("double1"), vo_xmlComboTemp, vo_xmlCombo, &(xmlGetFuncs::double1Get) );
    
    }
    
    
    
  5. In Topic: Store all combinations of children in a vector of structs

    Posted 19 Sep 2012

    Thanks a ton, that worked like a charm! I think I saw an algorithm like this years ago but had forgotten about it. My implementation (using my previous XML example) looks like this (I don't have access to C++11 or Boost at the moment):

    vector<xmlCombination> vo_xmlCombo, vo_xmlComboTemp;
    xml_node rootNode = xml_File.child("root"); //code to load the XML file into xml_File not shown here
    
    xml_node parNode = rootNode.child("string1");
    for (xml_node chNode =  parNode.first_child() ; chNode; chNode = chNode.next_sibling() ) {
        xmlCombination tempXMLCombo;
        tempXMLCombo.str1 = chNode.text().get();
        vo_xmlCombo.push_back(tempXMLCombo);
    }
    
    vo_xmlComboTemp.clear();
    for (vector<xmlCombination>::iterator iter = vo_xmlCombo.begin(); iter < vo_xmlCombo.end(); iter++) {
        xml_node parNode = settingsNode.child("int1");
        for (xml_node chNode =  parNode.first_child() ; chNode; chNode = chNode.next_sibling() ) {
            iter->int1 = chNode.text().as_int(); 
            vo_xmlComboTemp.push_back(*iter);
        }
    }
    
    vo_xmlCombo.clear();
    for (vector<xmlCombination>::iterator iter = vo_xmlComboTemp.begin(); iter < vo_xmlComboTemp.end(); iter++) {
        xml_node parNode = settingsNode.child("string2");
        for (xml_node chNode =  parNode.first_child() ; chNode; chNode = chNode.next_sibling() ) {
            iter->str2 = chNode.text().get(); 
            vo_xmlCombo.push_back(*iter);
        }
    }
    
    vo_xmlComboTemp.clear();
    for (vector<xmlCombination>::iterator iter = vo_xmlCombo.begin(); iter < vo_xmlCombo.end(); iter++) {
        xml_node parNode = settingsNode.child("double1");
        for (xml_node chNode =  parNode.first_child() ; chNode; chNode = chNode.next_sibling() ) {
            iter->dbl1 = chNode.text().as_double(); 
            vo_xmlComboTemp.push_back(*iter);
        }
    }
    
    vo_xmlCombo = vo_xmlComboTemp;
    
    

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

Tim Sloane hasn't added any friends yet.

Comments

Tim Sloane has no profile comments yet. Why not say hello?