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)?
STL container (such as a list or a vector)?
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)?
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)?
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.
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?
barcaroller said: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.
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.
Really? Where did that guarantee come from?
Whom "them"? What does your container look like?
<shrug> I would like to see that...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.
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);
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.