I am looking for the perfect data structure for the following scenario:
I have an index i
, and for each one I need to support the following operation 1: Quickly look up its Foo
objects (see below), each of which is associated with a double
value.
So I did this:
struct Foo {
int a, b, c;
};
typedef std::map<Foo, double> VecElem;
std::vector<VecElem> vec;
But it turns out to be inefficient because I also have to provide very fast support for the following operation 2: Remove all Foo
s that have a certain value for a
and b
(together with the associated double values).
To perform this operation 2, I have to iterate over the maps in the vector, checking the Foo
s for their a
and b
values and erasing them one by one from the map, which seems to be very expensive.
So I am now considering this data structure instead:
struct Foo0 {
int a, b;
};
typedef std::multimap<Foo0, std::map<int, double> > VecElem;
std::vector<VecElem> vec;
This should provide fast support for both operations 1 and 2 above. Is that reasonable? Is there lots of overhead from the nested container structures?
Note: Each of the multimaps will usually only have one or two keys (of type Foo0
), each of which will have about 5-20 values (of type std::map<int,double>
).