Thread: Assigning the content of one map to another map and doing the same with vector

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Assigning the content of one map to another map and doing the same with vector

    Ok, this is getting a little annoying...
    After spending 2 hours debugging, I have successfully inserted all enumerators (both string names and their values) into a map<string, int> in my ConvertEnumToStrings program. The output of my program proves this. However, now what's happening is, when I get the map, the resulting map that I use in the int main() function has all mapped values set to 0 (even though the original member map of the CenumOperations class stored several different values). The size() is correct, but all values are set to 0. The same is true with the vector of enumerator values. The key value side of the map (i.e. "string") has the right values, but the mapped values are all 0.

    Why would that be?

    I'm thinking it has something to do with the assignment operator '=' of the std::map class failing to work right. Basically, how this works is:

    1. A file containing an enum is passed to the program.
    2. The complete enum (along with various elements, such as a vector of its enumerators string names, and a vector of its enumerators int values, as well as the map already mentioned) is stored in memory (internally inside of members of the CenumOperations class).
    3. Now in int main(), I create an object of the class, call the CenumOperations::getEnumerationsMap() to retrieve the enumerations map. getEnumerationsMap returns a const map<string, int> reference to a copy of the original map (inside this function, the copy is assigned the content of the original, then the original map is cleared, and a reference to the copy is returned), i.e:

    Code:
    const map <string, int>& CenumOperations::getEnumeratorsMap() {
    
        enumeratorsMapCopy_m_si = enumeratorsMap_m_si; //get a copy of the enumerators map
        enumeratorsMap_m_si.clear(); //then clear the original
        return enumeratorsMapCopy_m_si; //and return a reference to the copy
    
    }
    Note that the enumerators map copy is also a member of the CenumOperations, same as the original map. Similiarly, the getEnumeratorValues() member function looks like this:

    Code:
    const vector <int>& CenumOperations::getEnumeratorValues() {
    
        enumeratorValuesCopy_v_i = enumeratorValues_v_i; //get a copy of the values of the enumerators
        enumeratorValues_v_i.clear(); //then clear the original
        return enumeratorValuesCopy_v_i; //and return a reference to the copy
    
    }
    Now, in int main(), what happens is, I create another map (and vector). I assign to the map the return value of getEnumeratorsMap(), and I assign to the vector the return value of getEnumeratorValues():

    Code:
    int main() {
      //do stuff...
      map <string, int> enumeratorsMap_m_si; //for storing a map of the enumerators
      vector <int> valuesOfEnumerators_v_i; //for storing the values of enumerators
      //do more stuff...
      enumeratorsMap_m_si = convertEnumToStringsObject.getEnumeratorsMap(); 
      //we now (actually, should...) have a map of both enumerator names and numerical values...BUT WE DON'T!! :D haha
      valuesOfEnumerators_v_i = convertEnumToStringsObject.getEnumeratorValues(); 
      //we now have a int vector of the values of the enumerators...or not :(
    }
    Note that the "convertEnumToStringsObject" is an object of a class that inherits from both the "CenumOperations" and "CfileOperations" classes, and has no members of its own.

    So can anyone tell me why my program outputs this:
    gorilla@ubuntu:~/Documents/Programming/Completed_Programs/ConvertEnumToStrings/bin/Debug$ ./ConvertEnumToStrings "/home/gorilla/Documents/test.cpp" "results"
    Thank you for running ConvertEnumToStrings.
    This program will work with either C or C++ enums.
    Before we begin converting enums to strings though,
    please take note of the following points:

    1. I did not provide support for invalid enums.
    2. I also did not provide support for enums which
    use variable names in any enumerator assignments.
    You must use only const value numbers when you explicitly
    assign an enumerator a value. The reason for this is, its
    not possible to figure out the internal value of a variable.
    3. The only exception to the last part of 2. is when
    you specified the type of enum, and its a different
    numerical type, such as a char. In this case, the program
    will output the numerical representation of whatever char
    has been assigned to a given enumerator. You are then free
    to convert it back to a char if you want.
    4. There are no other restrictions. The program should handle
    all other kinds of enums.

    Please press Enter to begin.


    Thank you. We will now get the converting process underway...

    currentEnumName_s is: DAY
    We found an enumerator.
    currentEnumeratorName_s is: sunday
    enumeratorsCounter_ui is: 1
    enumeratorsMap_m_si.find("sunday")->second is: 0
    We found an enumerator.
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 0
    currentEnumeratorName_s is: monday
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("monday")->second is: 0
    We found an enumerator.
    currentEnumeratorName_s is: tuesday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 0
    currentEnumeratorValue_i after incrementing: 1
    enumeratorsCounter_ui is: 3
    enumeratorsMap_m_si.find("tuesday")->second is: 1
    We found an enumerator.
    currentEnumeratorName_s is: wednesday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 1
    currentEnumeratorValue_i after incrementing: 2
    enumeratorsCounter_ui is: 4
    enumeratorsMap_m_si.find("wednesday")->second is: 2
    We found an enumerator.
    currentEnumeratorName_s is: thursday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 2
    currentEnumeratorValue_i after incrementing: 3
    enumeratorsCounter_ui is: 5
    enumeratorsMap_m_si.find("thursday")->second is: 3
    We found an enumerator.
    currentEnumeratorName_s is: friday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 3
    currentEnumeratorValue_i after incrementing: 4
    enumeratorsCounter_ui is: 6
    enumeratorsMap_m_si.find("friday")->second is: 4
    We found an enumerator.
    currentEnumeratorName_s is: saturday
    The current enumerator is not the first and does not have an assigned value.
    currentEnumeratorValue_i before incrementing: 4
    currentEnumeratorValue_i after incrementing: 5
    enumeratorsCounter_ui is: 7
    enumeratorsMap_m_si.find("saturday")->second is: 5
    namesOfEnumerators_v_s.size() is: 7
    valuesOfEnumerators_v_i.size() is: 7
    enumeratorsMap_m_si.size() is: 7
    numOfEnumerators_ui is: 7
    We found a single-line enum.
    currentEnumName_s is: AREYOUAROCK
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 1
    currentEnumeratorName_s is: yes
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("yes")->second is: 1
    We're at the last enumerator of the single-line enum.
    We found an enumerator that has an assigned value.
    currentEnumeratorValue_i is: 0
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("no")->second is: 0
    namesOfEnumerators_v_s.size() is: 2
    valuesOfEnumerators_v_i.size() is: 2
    enumeratorsMap_m_si.size() is: 2
    numOfEnumerators_ui is: 0
    currentEnumName_s is: Work
    We found an enumerator.
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 1
    currentEnumeratorName_s is: hard
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("hard")->second is: 1
    We're at the last enumerator of the enum.
    The last enumerator of the enum has an assigned value.
    currentEnumeratorValue_s is: 2
    currentEnumeratorValue_i is: 2
    currentEnumeratorName_s is: lazy
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("lazy")->second is: 2
    namesOfEnumerators_v_s.size() is: 2
    valuesOfEnumerators_v_i.size() is: 2
    enumeratorsMap_m_si.size() is: 2
    numOfEnumerators_ui is: 2
    The current line with 'enum' has a comment.
    currentEnumName_s is: weirdenum
    We found an enumerator.
    currentEnumeratorName_s is: weirdenum1
    enumeratorsCounter_ui is: 1
    enumeratorsMap_m_si.find("weirdenum1")->second is: 0
    We found an enumerator.
    We found an enumerator with an assigned value.
    currentEnumeratorValue_i is: 2
    currentEnumeratorName_s is: weirdenum2
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("weirdenum2")->second is: 2
    We're at the last enumerator of the enum.
    The last enumerator of the enum has an assigned value.
    currentEnumeratorValue_s is: 0
    currentEnumeratorValue_i is: 0
    currentEnumeratorName_s is: weirdenum3
    currentEnumeratorHasAssignedValue_b is true.
    enumeratorsMap_m_si.find("weirdenum3")->second is: 0
    namesOfEnumerators_v_s.size() is: 3
    valuesOfEnumerators_v_i.size() is: 3
    enumeratorsMap_m_si.size() is: 3
    numOfEnumerators_ui is: 3
    4 enums were found.
    They were successfully written to file.
    Generated file can be found at: /home/gorilla/Documents/results.h

    If you chose to write to a header file, then you
    can include the header in your source file, and
    call up a function to retrieve the names of all
    the enumerations or a specific enumeration you choose.
    Otherwise if you wrote to a source file, you will need
    to put the implemention in the source file you wrote to.
    The generated file provides the following functions inside
    a class with the name of 'CnameofenumToStrings':

    const vector<string>& getEnumeratorNames();
    const vector<int>& getEnumeratorValues();
    const string& getNameOfEnum();
    const int& getNumOfEnumerators();
    const map<string, int>& getEnumeratorsMap();
    const string& getEnumeratorValue(nameofenum enumerator);

    where 'nameofenum' is replaced with the actual name of the
    enum you want to retrieve something from.

    The function names are pretty self-explanatory:

    getEnumeratorNames() obviously returns a reference
    to a vector of the string names of the enumerators in
    your enum.
    getEnumeratorValues() returns a reference to a vector
    of the int values of the enumerators in your enum.
    getNameOfEnum() is provided in case you need to retrieve
    the string name of the enum for some reason. It returns
    a reference to a string.
    getNumOfEnumerators() returns a reference to an int containing
    the total number of enumerators found in the enum, in case
    you need that for some reason.
    getEnumeratorsMap() returns a reference to a map<string, int>
    containing the string names and int values of the enumerators in an enum.
    The int values can be either explicit or implicit, and the type of the enum does
    not even have to be int. It can be some other intergal type, but
    ConvertEnumToStrings will always return it in int form.
    getEnumeratorValue(nameofenum enumerator) returns a
    reference to a int containing the value of the enumerator
    you passed in the parameter.

    Congratulations, and feel free to use this program again anytime.


    Press Enter to end this program.
    Haha, I figured everyone would love the really long output...
    Note that most of the output is debug output statements (and they demonstrate that I was able to get the actual enumerator values and insert them into the map, since calling find() on them after inserting returns the right values). I'll remove those before I'm done with the program.
    Last edited by Programmer_P; 07-11-2010 at 09:08 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed