3d Vector

Well I have attempted to make my version of a 3d vector and I got a really strange error in the STL files on line 485.
Assuming EveryThing Else is taken care of:
1
2
3
4
	extern std::vector<std::vector<std::vector<short int>>> TileMap;
	  TileMap.resize(XSize);
	  TileMap[XSize].resize(YSize);
	  TileMap[XSize][YSize].resize(ZSize);

Thanks in advance
-Internet is bad; Don't Expect a speedy reply-
First, what was the error?

Are you using C++11? Because in earlier standards in the declaration of a 2d vector, a >> is interpreted as the >> operator. To fix this one has to do > >. Is the same thing happening with your code, with the >>>?

This has been fixed in C++11, not sure whether it handles >>> though.

Apart from that, the memory management would be horribly inefficient if any of the vectors need to be resized. Can you have a <list> of Rooms? Do Rooms have to vary in size - can they be a static size?

If you truly want a 3d space, would you not be better to use 3d positions?
Your setting the size wrong
1
2
3
4
5
6
7
8
std::vector<std::vector<std::vector<short int>>> TileMap;
	TileMap.resize(XSize);
	for(int i = 0; i < XSize; ++i)
	{
		TileMap[i].resize(YSize);
		for(int j = 0; j < XSize; ++j)
			TileMap[i][j].resize(ZSize);
	}
Why call resize at all? Vectors are perfectly capable of sizing themselves as needed during construction.

1
2
3
4
5
6
7
8
    std::vector<std::vector<std::vector<short>>> TileMap
    (
        XSize,
        std::vector<std::vector<short>>(
            YSize,
            std::vector<short>(ZSize)
        )
    );

Although I find vectors-of-vectors-of-vectors to be awkward to use. Consider boost.multi_array http://www.boost.org/doc/libs/release/libs/multi_array/doc/index.html or your own class that holds a 1D-vector and does index translation in the accessors.
Ok, Sorry about the lack of detail:

No I'm not using C++11 (Is it worth getting?)

The error was on line 585 in the yvar.h file
'Missing ; before namespace'

The Reason Im looking for a 3d sort of tile map is that in a 3d space I could theoretically make every cube in a voxel engine 1 value in the space, however it would seem very impractical.

I took a look at the boost library, it looks useful thats what Ill most likely use for this project, so thanks a lot for that reference.

One thing, does boost support resizing arrays as easily as vectors?
Of course:

The boost::multi_array class provides an element-preserving resize operation. The number of dimensions must remain the same, but the extent of each dimension may be increased and decreased as desired.


http://www.boost.org/doc/libs/release/libs/multi_array/doc/user.html#sec_resize
Last edited on
Topic archived. No new replies allowed.