473,395 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Better way to do this? (enum or map?)

I am creating a program that uses a lot of enumerations and associated
arrays of strings that look like this:

enum Enum1
{
E1_NONE = -1,
E1_A = 0,
E1_B = 1,
E1_END = 2
}
const string Enum1Str[E1_END]={"Val 1","Val 2"};

I was thinking about creating a derived map<string, intclass to
handle this better, but if I were to do so, I would need the following
methods added to it (though I'm not 100% familiar with iterators, so
the return values for KeyList and ValueList may not be correct):

vector<string>::iterator KeyList();
vector<int>::iterator ValueList();
string KeyOf(int val);

I think if I all I had were simple enumerations such as the one above,
the derived class would be overwhelmingly preferable, but I also have
enumerations as such:

enum Enum2
{
E2_NONE = -1,
E2_A = 0,
E2_B = 1,
E2_C = 2,
E2_D = 3,
// One possibility
E2_E = 4,
E2_END_1 = 5,
// Fork off different options
E2_F = 4,
E2_G = 5,
E2_END_2 = 6,
}
const string Enum2Str1[E2_END_1]={"Ref 1","Ref 2","Ref 3","Ref 4","Ref
5A"};
const string Enum2Str2[E2_END_2]={"Ref 1","Ref 2","Ref 3","Ref 4","Ref
5B","Ref 6B"};

which are there for double duty because sometimes values overlap when
different options are set.

Would it be in my best interest to go with the class and split up the
dual-duty enumerations or find some way to extend the class even
further to allow for multiple "ends" of the allowable options? If that
would be that case, how exactly might I go about doing that? Thanks in
advance.

--MathStuf

Feb 24 '07 #1
3 2080
* MathStuf:
I am creating a program that uses a lot of enumerations and associated
arrays of strings
Consider whether you can replace enums with types.

E.g., instead of

enum Kind{ a, b, c };

struct Something { Kind kind; ... };

void foo( Something& x }
{
switch( x.kind )
{
case a: doA( x ); break;
case b: doB( x ); break;
case c: doC( x ); break;
}
}

int main()
{
Something sth;
sth.kind = c;
foo( sth );
}

try

struct Something
{
...
virtual void foo() = 0;
};

struct A: Something { void foo() { /* like doA */ } };
struct B: Something { void foo() { /* like doB */ } };
struct C: Something { void foo() { /* like doC */ } };

int main()
{
C sth;
sth.foo();
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 24 '07 #2
MathStuf wrote:
I am creating a program that uses a lot of enumerations and associated
arrays of strings that look like this:

enum Enum1
{
E1_NONE = -1,
E1_A = 0,
E1_B = 1,
E1_END = 2
}
const string Enum1Str[E1_END]={"Val 1","Val 2"};

I was thinking about creating a derived map<string, intclass to
handle this better, but if I were to do so, I would need the following
methods added to it (though I'm not 100% familiar with iterators, so
the return values for KeyList and ValueList may not be correct):
Derivation is not a good idea for this case. You should write a class
that contains a map as a data member, and expose just the methods you
need instead of thinking in terms of adding to the existing map iterface
(most of which you won't need, some of which would probably actually be
harmful).
>
vector<string>::iterator KeyList();
vector<int>::iterator ValueList();
string KeyOf(int val);
KeyList and ValueList are not natural fits with a map. If you really
need these you would have to store seperate data member to perform
implement this. So you class would hold a map, plus two vectors. In fact
it would problably be simpler to drop the map and store two vectors,
with the key list vector being sorted for fast access. I'm assuming here
that once an enumeration class has been initialised you wouldn't need to
change it thereafter.
>
I think if I all I had were simple enumerations such as the one above,
the derived class would be overwhelmingly preferable, but I also have
enumerations as such:

enum Enum2
{
E2_NONE = -1,
E2_A = 0,
E2_B = 1,
E2_C = 2,
E2_D = 3,
// One possibility
E2_E = 4,
E2_END_1 = 5,
// Fork off different options
E2_F = 4,
E2_G = 5,
E2_END_2 = 6,
}
const string Enum2Str1[E2_END_1]={"Ref 1","Ref 2","Ref 3","Ref 4","Ref
5A"};
const string Enum2Str2[E2_END_2]={"Ref 1","Ref 2","Ref 3","Ref 4","Ref
5B","Ref 6B"};

which are there for double duty because sometimes values overlap when
different options are set.

Would it be in my best interest to go with the class and split up the
dual-duty enumerations or find some way to extend the class even
further to allow for multiple "ends" of the allowable options? If that
would be that case, how exactly might I go about doing that? Thanks in
advance.
I really think you have to stop thinking in terms of derivation (or
extension) as means of solving these problems. Instead think of one or
more classes containing whatever appropriate data structures you need.
>
--MathStuf
john
Feb 24 '07 #3
On Feb 24, 6:14 am, John Harrison <john_androni...@hotmail.comwrote:
MathStufwrote:
I am creating a program that uses a lot of enumerations and associated
arrays of strings that look like this:
enum Enum1
{
E1_NONE = -1,
E1_A = 0,
E1_B = 1,
E1_END = 2
}
const string Enum1Str[E1_END]={"Val 1","Val 2"};
I was thinking about creating a derived map<string, intclass to
handle this better, but if I were to do so, I would need the following
methods added to it (though I'm not 100% familiar with iterators, so
the return values for KeyList and ValueList may not be correct):

Derivation is not a good idea for this case. You should write a class
that contains a map as a data member, and expose just the methods you
need instead of thinking in terms of adding to the existing map iterface
(most of which you won't need, some of which would probably actually be
harmful).
vector<string>::iterator KeyList();
vector<int>::iterator ValueList();
string KeyOf(int val);

KeyList and ValueList are not natural fits with a map. If you really
need these you would have to store seperate data member to perform
implement this. So you class would hold a map, plus two vectors. In fact
it would problably be simpler to drop the map and store two vectors,
with the key list vector being sorted for fast access. I'm assuming here
that once an enumeration class has been initialised you wouldn't need to
change it thereafter.


I think if I all I had were simple enumerations such as the one above,
the derived class would be overwhelmingly preferable, but I also have
enumerations as such:
enum Enum2
{
E2_NONE = -1,
E2_A = 0,
E2_B = 1,
E2_C = 2,
E2_D = 3,
// One possibility
E2_E = 4,
E2_END_1 = 5,
// Fork off different options
E2_F = 4,
E2_G = 5,
E2_END_2 = 6,
}
const string Enum2Str1[E2_END_1]={"Ref 1","Ref 2","Ref 3","Ref 4","Ref
5A"};
const string Enum2Str2[E2_END_2]={"Ref 1","Ref 2","Ref 3","Ref 4","Ref
5B","Ref 6B"};
which are there for double duty because sometimes values overlap when
different options are set.
Would it be in my best interest to go with the class and split up the
dual-duty enumerations or find some way to extend the class even
further to allow for multiple "ends" of the allowable options? If that
would be that case, how exactly might I go about doing that? Thanks in
advance.

I really think you have to stop thinking in terms of derivation (or
extension) as means of solving these problems. Instead think of one or
more classes containing whatever appropriate data structures you need.
--MathStuf

john
Thanks for the help. I have decided to go with a class with two
vector, one for keys, the other values and allow the subscript
operator to be used both ways.

--MathStuf

Feb 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: wogston | last post by:
(A) template <typename T> metainline foo<T> lerp(const foo<T>& a, const foo<T>& b, const T& time) { foo<T> v; repeat< 4,exec_mul<T> >::exec(v,a,b,time); return v; }
3
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
9
by: John Salerno | last post by:
This is where my OO skills aren't quite honed yet. Some of you might remember the little game program I was working on for fun. I had several methods written to determine a character's class, race,...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
2
by: octangle | last post by:
Create enumerated values using the three highest bits as bit flags that define comparison operators. VERSION A: ---------- enum eCriteria { eCriteriaOne = 1, eCriteriaTwo = 2,
22
by: JoeC | last post by:
I am working on another game project and it is comming along. It is an improvment over a previous version I wrote. I am trying to write better programs and often wonder how to get better at...
3
by: ankitks | last post by:
hello, I have a question regarding how to include classes . Assuming I have a class called class paraClass { enum eType { eOne, eTwo, eThree }; enum cValue = 4; static convertToString (int...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.