简体   繁体   中英

how to assign the enum values to strings using map in C++

I have a map with string and enum. I am getting an enum value as input and from that I need to get a string.

Below is the code snippet I have written for that:

std::map<std::string, Messages> m_MessagesMap;

auto MessageID = GetMessageID();
GetStringFromEnum(MessageID);
std::string CJsonMessageUtil::GetStringFromEnum(Messages l_eMessages)
{
    for (auto it = m_MessagesMap.begin(); it != m_MessagesMap.end(); ++it)
    {
        if (it->second == l_eMessages)
            return it->first;
    }

    return "";
}

This way it is working. But every time it is looping through all the items in the map. Is there any better way to map enums to strings?

If you do not need that map for something else, then the easiest would be this, as suggested by @Casey:

std::string
getString( EnumType et )
{
  switch( et )
  {
   case et1: return "string1";
   case et2: return "string2";
   ...
   default: assert(0);
  }
}

But if you do need that map elsewhere (and of course not duplicate its content), you can use a bimap . Boost provides one, see boost::bimap . Its header-only, so pretty easy to integrate in your code.

There are some subtleties, so here is a quick example ( runnable here ):

#include <boost/bimap.hpp>
enum MyEnum
{
    hello, world
};

using MyMap = boost::bimap<std::string,MyEnum>;

int main()
{   
    MyMap m;
    m.insert( MyMap::value_type("hello", hello ) );
    m.insert( MyMap::value_type("world", world ) );

    std::cout << m.right.at( hello );
}

If you have many values in that map, it could be worth it. And I'm sure initializing the values can be done in a much more efficient way, but I can't search that at present.

You could use a lookup table:

enum Trees {REDWOOD, MAPLE, OAK, SYCAMORE, EUCALYPTUS};
struct Enum_Conversion_Table
{
    enum Trees value;
    const char * text;
};

static const Enum_Conversion_Table table[] =
{
    {REDWOOD, "Redwood"},
    //...
    {EUCALYPTUS, "Eucalyptus"},
};
static const unsigned int table_size =
    sizeof(table) / sizeof(table[0]);

The table data type allows you to search by enum to find the text, or to search by the text to get the enum value.

You can also change the size of the table without changing the search code.

Since the table is tagged as static const , it will be initialized before run-time or accessed directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM