enum -> STL container

B

barcaroller

What is a good (or standard) way of moving the contents of an enum into an
STL container (such as a list or a vector)?
 
V

Victor Bazarov

barcaroller said:
What is a good (or standard) way of moving the contents of an enum
into an STL container (such as a list or a vector)?

What's "the contents of an enum"? Is that like "all the constants
in my current scope" or "all macros currently defined in the module"?
I'm asking because the enumerators defined inside an enumeration type
are essentially that, a set of named constants that all have some
specific type (and values), there is no specific order or containment
relationship between them or between them and the enumeration.

V
 
J

Jim Langston

barcaroller said:
What is a good (or standard) way of moving the contents of an enum into an
STL container (such as a list or a vector)?

I don't quite understand the question. Given the enum:

enum Foo
{
Zero,
One,
Two
};

What is it you want to add to the vector? Instances of Foo? "Zero", "One",
"Two"? Zero, One, Two? 0, 1, 2?

The first is trivial.

#include <vector>

enum Foo
{
Zero,
One,
Two
};

int main()
{
std::vector<Foo> Bar;
Bar.push_back( Zero );
Foo Val = One;
Bar.push_back( Val );
}

Anything else depends on what you are attempint to do. There was a thread,
recently, about how to get the text strings of an enum, which you could use
for the second. Anything else, however, doesn't seem like it would achieve
you anything, so what is it you are trying to accomplish in the end?
 
B

barcaroller

Victor Bazarov said:
What's "the contents of an enum"? Is that like "all the constants
in my current scope" or "all macros currently defined in the module"?
I'm asking because the enumerators defined inside an enumeration type
are essentially that, a set of named constants that all have some
specific type (and values), there is no specific order or containment
relationship between them or between them and the enumeration.

The enum contains a list of integers (constants). Something like enum {
MIN, ONE=13, TWO=45, THREE=55, MAX }

Basically, a bunch of "#defines". The only guarantee is that they are in
ascending order. I want to move them into an STL container so that I can
iterate over them without having to use a switch statement every time. My
guess is that I would have to use at least one switch statement to move them
into the STL container.
 
B

barcaroller

Jim Langston said:
I don't quite understand the question. Given the enum:

enum Foo
{
Zero,
One,
Two
};

What is it you want to add to the vector? Instances of Foo? "Zero",
"One", "Two"? Zero, One, Two? 0, 1, 2?

The first is trivial.

#include <vector>

enum Foo
{
Zero,
One,
Two
};

int main()
{
std::vector<Foo> Bar;
Bar.push_back( Zero );
Foo Val = One;
Bar.push_back( Val );
}

Anything else depends on what you are attempint to do. There was a
thread, recently, about how to get the text strings of an enum, which you
could use for the second. Anything else, however, doesn't seem like it
would achieve you anything, so what is it you are trying to accomplish in
the end?

Please see earlier message to Victor.
 
V

Victor Bazarov

barcaroller said:
The enum contains a list of integers (constants). Something like
enum { MIN, ONE=13, TWO=45, THREE=55, MAX }

It contains a "list of integers" as much as a function declaration
contains the list of arguments or a class definition contains a list
of member declarations. There is no such thing at the _run-time_.
It all exists in the source code, and ONLY in the source code.
Basically, a bunch of "#defines". The only guarantee is that they
are in ascending order.

Really? Where did that guarantee come from?

enum { foo = 3, bar = 2, foobar = 1 };
I want to move them into an STL container so

Whom "them"? What does your container look like?
that I can iterate over them without having to use a switch statement
every time. My guess is that I would have to use at least one switch
statement to move them into the STL container.

<shrug> I would like to see that...

What you need is a duplication of your enum declaration in an array.

enum { MIN, ONE=13, TWO=45, THREE=55, MAX };
int const enum_copies[] = {MIN, ONE, TWO, THREE, MAX};
std::list<int> my_enum(enum_copies, enum_copies + 5);

V
 
B

barcaroller

Victor Bazarov said:
It contains a "list of integers" as much as a function declaration
contains the list of arguments or a class definition contains a list
of member declarations. There is no such thing at the _run-time_.
It all exists in the source code, and ONLY in the source code.

Yes, I understand that.

Really? Where did that guarantee come from?

From me. I add new constants as needed.

Whom "them"? What does your container look like?

My container will contain the integers (13, 45, 55, ...). MIN and MAX will
not be included.

that I can iterate over them without having to use a switch statement
every time. My guess is that I would have to use at least one switch
statement to move them into the STL container.
<shrug> I would like to see that...
What you need is a duplication of your enum declaration in an array.

enum { MIN, ONE=13, TWO=45, THREE=55, MAX };
int const enum_copies[] = {MIN, ONE, TWO, THREE, MAX};
std::list<int> my_enum(enum_copies, enum_copies + 5);

Yes, this is what I need to do (so why the shrug above?) I thought there
was a way I could do it without using the array in the middle, but it looks
like there isn't. Thank you (and Jim).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top