maping two values via enum

Hi

I've two values
eg

enum
{
zero_t = 0,
one_t,
two_t,
}
from one funciton I'll get this enum value.

but acutal string value of zero_t = "ZERO"
i need to convert that zero_t to "ZERO".

On otherway I'llget a string "ZERO" i need to convert to zero_t.

How to do this??

You can't stick a string in an enum, but here is what you can do:

1
2
3
4
5
6
7
8
9
enum eNumber
{
  zero_t = 0,
  one_t,
  two_t,
  eNumbersize,
};

string NumStrings[eNumbersize] = { "ZERO", "ONE", "TWO" };


Then to convert your enum into a string:
cout << NumStrings[zero_t];

this will give zero_t to ZERO.

But I need
"Zero" to zero_t also.

both viseversa I need.

basically encoding and decoding.
If you don't want boost, this could work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <vector>
#include <utility>

enum eNumber
{
    zero_t ,
    one_t,
    two_t,
};

template <class Left, class Right>
class bimap
{
    typedef std::pair<Left,Right> _PAIR;
    typedef std::vector<_PAIR>    _SET;
    _SET set;
public:
    void push_back(const Left l, const Right r)
    {
        set.push_back( _PAIR (l,r) );
    }
    Left getLeft(Right r)
    {
        for ( _SET::iterator it = set.begin(); it != set.end(); ++it)
            if (it->second == r)
                return it->first;
        return set.begin()->first;
    }
    Right getRight(Left l)
    {
        for ( _SET::iterator it = set.begin(); it != set.end(); ++it)
            if (it->first == l)
                return it->second;
        return set.begin()->second;
    }
};


int main()
{
    bimap<eNumber, std::string> NumSet;
    
    NumSet.push_back(zero_t,"ZERO");
    NumSet.push_back(one_t,"ONE");
    NumSet.push_back(two_t,"TWO");

    std::cout << NumSet.getRight(zero_t) << std::endl;
    std::cout << NumSet.getLeft("ONE") << std::endl;
    
    return 0;
}
ZERO
1
Last edited on
And here it is a little more specified (less generalized) for readability if you don't plan to port it to other stuff:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <vector>
#include <utility>

enum eNumber
{
    zero_t ,
    one_t,
    two_t,
    inval_t,
};

class bimap
{
    typedef std::pair<eNumber,std::string> _PAIR;
    typedef std::vector<_PAIR>    _SET;
    _SET set;
public:
    void push_back(const eNumber n, const std::string s)
    {
        set.push_back( _PAIR (n,s) );
    }
    eNumber getNum(const std::string s)
    {
        for ( _SET::iterator it = set.begin(); it != set.end(); ++it)
            if (it->second == s)
                return it->first;
        return inval_t;
    }
    std::string getStr(const eNumber n)
    {
        for ( _SET::iterator it = set.begin(); it != set.end(); ++it)
            if (it->first == n)
                return it->second;
        return "";
    }
};


int main()
{
    bimap NumSet;
    
    NumSet.push_back(zero_t,"ZERO");
    NumSet.push_back(one_t,"ONE");
    NumSet.push_back(two_t,"TWO");

    std::cout << NumSet.getStr(zero_t) << std::endl;
    std::cout << NumSet.getNum("ONE") << std::endl;
    
    return 0;
}
ZERO
1
Last edited on
Topic archived. No new replies allowed.