Please explain how you intend to use the container and we will make our best attempts to find a nice solution. As of now, your usage looks a little bit nuts.
If you want to lookup a Foo pointer given a 3D coordinate, or something similar, why would you want logarithmic lookup when constant lookup is available? For example, nested vectors could provide a 3D array-like container that will outperform the map approach. One downside is that a system of floating-point coordinates would make the container huge...
vector<> should be used when the extent is not known at compile time. In this case, it is always 3, so even
boost::array<> is better than std::vector<>, but creating a simple struct like Abramus gave is better since
it makes the code more expressive.
It seems to me that it depends on how this data is t be accessed. If you want to find a Foo* given three coordinates then the map looks very inefficient:
1 2 3 4 5 6 7 8 9 10 11
map <vector<short>, Foo *> Foos;
Foo* get_foo(int x, int y, int z)
{
// search Foos for a match? Takes ages
}
// alternatively:
vector <vector <vector < Foo*> > > Foos;
Foo* get_foo(int x, int y, int z)
{
return Foos[x][y][z]; // fast efficient
}
You would be much better with the vector of vector of vector. But if you simply want to record the coordinate of each Foo* then I would be tempted to make a struct with your coords and your Foo* together and put them in a vector:
1 2 3 4 5 6
struct FooRec
{
int x, y, z;
Foo* foo;
};
std::list<FooRec> Foos; // record where each Foo is, no need to search based on location
It really depends why this data is together and how you want to access it.
The problem with the above approach is that it consumes (range_of_x) * (range_of_y) * (range_of_z) * sizeof( Foo* ) bytes
of memory just for the pointers. So if the coordinate system is bounded by (0,0,0) and (99,99,99) then you have
100*100*100 = 1,000,000 pointers. If the bounds are (0,0,0) and (999,999,999) then you're over 4GB of data.
Thanks for your replies. I think that I'll go with the struct solution combined with the 3d vector.
My code is trying to simulate the behavior of particle with a certain potential between them.
Each particle can move around freely as long as no forces are acting on it. The Forces are decreasing with the distance (much like the gravitational force) and I have a cutoff distance. My system is divided into boxes and I calculate the interactions between all the balls in each two neighboring boxes (as well as within a single box). Since I have roughly 50*50*50 boxes,I have to access the map 50*50*50*14=1750000 times.
running time of about 10 cycles per second is reasonable to me (there are other parameters to consider apart from the immediate interaction)
If you only need sequential access to the data, why not use a list? You could implement a box as a linked list of balls and use the splice member function to move the balls from one box to another when necessary.