简体   繁体   中英

Represent a key value as an enum

I am reading a value from a config file:

txtype=value

value can be one of four values: transmit, receiver, transceiver, any. There is lots of existing code for reading the key value pairs from the file so I just have to represent it as a type.

I wanted to represent this as an enum:

 enum txtype { transmit = "transmit", receiver = "receiver", transceiver = "transceiver", any = "any" }

but I realise now that I can't do that with c++ 98. Are there alternative ways of doing this in c++ 98?

It will really depend on what your compiler will support. If your compiler supports map , then you can simply create a map between you string and the integer index which you can assign as the enum value using std::map<std::string, int> . The enum is omitted below since you can define and declare an instance to assign the index returned as you like. A short example using map could be, eg

#include <iostream>
#include <string>
#include <map>

int main (void) {

    std::map<std::string, int> m = {
        {"transmit", 0},
        {"receiver", 1},
        {"transceiver", 2},
        {"any", 3}
    };
    std::string s;

    while ((std::cin >> s))
        std::cout << s << " - " << m[s] << '\n';
}

( note: if you are using Visual C++ 12 or earlier, you cannot use the {...} universal initializer)

Example Use/Output

$ printf "receiver\ntransceiver\nany\ntransmit\n" | ./bin/map_str_int
receiver - 1
transceiver - 2
any - 3
transmit - 0

If your compiler does not support map , you can do the same thing using std::string and a simple function to compare with the contents of an array of std::string returning the index of the matching type, eg

#include <iostream>
#include <string>

const std::string txstr[] = { "transmit",
                            "receiver",
                            "transceiver",
                            "any" };

const int ntypes = sizeof txstr / sizeof *txstr;

int gettxtype (const std::string& s)
{
    int i;

    for (i = 0; i < ntypes; i++)
        if (txstr[i] == s)
            return i;

    return -1;
}

int main (void) {

    std::string s;

    while ((std::cin >> s)) {
        int type =  gettxtype(s);
        if (type >= 0)
            std::cout << s << " - " << type << '\n';
    }
}

(as a benefit above, you can determine if the type provided does not match any of the known txtypes by returning -1 if the matching type is not found.)

The Use/Output is the same. Look things over and let me know if you have further questions.

I dont think it is possible with enum .But Hashtable will help you.

Hashtable has key value pair.

Assigning value:

A[transmit]="transmit" ...

Accesing :

string x;

x=A[transmit] will return transmit.

Here A is your Hashtable name.

https://en.cppreference.com/w/cpp/container/unordered_map

https://en.cppreference.com/w/cpp/utility/hash

Well, you can fake it in some way: Just take advantage of the fact that enumerations in C/C++ are really just numbers, so use your default numbering (as you already have it). When you read the value as string, search in a std::array or a std::map for the according mapping and use the index.

In pseudocode:

valueAsString <- readStringFromFile;  // your logic
idx <- getIndexFromMap[valueAsString];
yourEnum = static_cast<txtype>(idx);

Edit:

Of course a direct mapping from the string representations to the enumeration values would be prefereable here:

std::map<std::string, txtype> mapping = { /* initialize */ };

for (auto&& key : keys) {
  process(mapping[key]);
}

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