0

I created map that connects enum with strings in order to have easier times doing current project. Code looks like this:

struct responseMap : public std::map < std::string, CitizenType > {
    responseMap() {
        this->operator[]("slave") = CitizenType::slave;
        this->operator[]("trader") = CitizenType::trader;
        this->operator[]("priest") = CitizenType::priest;
        this->operator[]("scholar") = CitizenType::scholar;
        this->operator[]("soldier") = CitizenType::soldier;
        this->operator[]("archer") = CitizenType::archer;
        this->operator[]("swordsman") = CitizenType::swordsman;
        this->operator[]("emperor") = CitizenType::emperor;
    }
};

My enum is just these types, described in map. Everything works fine exept it doesn't count "slave" as a member of map. For instance if I write down the following code:

responseMap myMap;
std::cout<<myMap["slave"]<<" "<<myMap["trader"];

it would produce the following line: 0 1

My question is why this is happening as I am describing them the same way?

Edit: I am trying with normal map like this:

std::map<std::string, CitizenType> otherMap = {
    { "slave", CitizenType::slave },
    { "trader", CitizenType::trader },
    { "priest", CitizenType::priest },
    { "scholar", CitizenType::scholar },
    { "soldier", CitizenType::soldier },
    { "archer", CitizenType::archer },
    { "swordsman", CitizenType::swordsman },
    { "emperor", CitizenType::emperor },
};

And it still outputs the same values.

5
  • 3
    I bet the numeric value of CitizenType::slave is 0, and the numeric value of CitizenType::trader is 1. Which is what you see printed. What again seems to be the problem? Commented May 3, 2015 at 18:44
  • 1
    You can do (*this)["slave"]= instead of this->operator[]("slave")= Commented May 3, 2015 at 18:46
  • 1
    Very happy to see the civilization of society since slaves are also considered citizens now. Commented May 3, 2015 at 18:47
  • @texasbruce Only three fifths of a citizen. Commented May 3, 2015 at 18:50
  • Ok, I think I have mistaken what I need to do. Basically I have to chech whether CitizenType::[insert word here] is a member of the current enum Commented May 3, 2015 at 18:51

1 Answer 1

3

If you don't set it differently, enum will index from 0 - so it isn't ignoring your value, your value is 0. You may want to look at this, especially the part about "If the first enumerator does not have an initializer, the associated value is zero. For any other enumerator whose definition does not have an initializer, the associated value is the value of the previous enumerator plus one."

Sign up to request clarification or add additional context in comments.

4 Comments

Yea, I found what I have mistaken. Thanks for the response. So I'm trying to figure out a nice way to chech whether CitizenType::[insert word here] is a member of the current enum
Basic idea will be to check if the index of your string belongs to the range of enum: you need to save the number of elements in enum (because you can't check the highest value) and then check if myMap[myString] >= 0 and myMap[myString] < numberOfElems
I kinda did the same thing but with some difference. Set the start to be equal to 1 and just did the following if(myMap[word]) so it will return 0 when it doesn't exist and 1 or so when exists.
Sure, that's a nice solution, if you are doing the indexing from 1. My idea was for the case you leave it from 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.