If your enum
values are contiguous, simply cast them to std::size_t
.
std::vector<std::vector<double>> MyContainer;
// resize it
double d = MyContainer.at(std::size_t(Color::Red)).at(3)
now, you'll have to make sure MyContainer
has been resized, as had the vector
s inside it. at
does not fill in the container, it simply checks bounds and throws if it is out of bounds on vector
.
Now, as your set of points is fixed, we can do away with one vector
.
enum class Color {
Red,
Green,
Blue,
count, // automatically becomes the number of elements
};
std::array< std::vector<int>, std::size_t(Color::count) > MyContainer;
MyContainer[std::size_t(Color::Red)].push_back(3);
Another alternative is to replace the outermost container with std::unordered_map
, but that will be slower and less efficient. std::map
will be even slower.
If you use enum
instead of enum class
, you'll save some of the above std::size_t
casts, but I would advise against it. unordered_map
needs a quick hash:
namespace std {
template<>
struct hash<Color> {
std::size_t operator()(Color c) const {
return static_cast<std::size_t>(c);
}
};
}
but again, I'd advise against this path.
A final option would be to write a custom container wrapper around array
or vector
that does the std::size_t
casting for you. I would only bother doing that kind of thing if I was going to be publishing it as a library of some kind, or if I was bored.