I feel like this is a very simple, almost stupid question. I simply have forgotten what I should know how to do.
I have GameObjects, which are stored in a universal GameObjectManager. These GameObjects have an ObjectID, which is a simple int.
I need to be able to insert or remove these GameObjects in a 2D tile based world, where each TILE holds a container of ObjectID's.
This way, I can grab a specific tile (ex. Tile[10][10]) and then see what GameObjects are on Tile[10][10] by reading from the container. (ex. "Ah, so Character#4302 and Item#123 are on Tile[10][10]!")
Right now, each "Tile" is a structure in a MAP array of Tiles.
struct MapTile
{
std::vector <int> GameObject_MapList ; //list of Objects on this map location
TileTerrainType tileTerrainType; //GFX to display Grass, Sand, Water, Swamp, etc.
};
MapTile mlMap[100][100]; //map array
However, I read that Vectors should not be used when arbitrarily adding/removing variables. It is not like I will be removing the first or last variable in the array. Instead, I will have to call a specific ObjectID and remove it from wherever it is in the array.
For this reason, I was thinking of using a different container. Maybe I am just too tired, but I could use some advice on what container to use, and how to go about removing SPECIFIC variables in the container.
For example, GameObject has an ObjectID of "422". Tile[2][8] holds a container that has the following integers in order: 420, 421, 422, 433, 486, 800.
I have a function: RemoveGameObjectFromMap(int ObjectID); So I need it to remove 422, whenever I type (RemoveGameObjectFromMap(422);
I used to use a MAP container, but that is unnecessary. Vector, according to cplusplus.com would be bad for this.
Whether a vector is good or bad depends on a number of things. Like how many IDs per tile (on average), how frequent insertions, deletions and lookups are, how much need there is to minimise memory use, and a 100 other considerations. In short there are no simple answers. The obvious alternative to std::vector
in this case though would be std::unordered_set
(I'm assuming that you never want the same ID in the same tile more than once).
If you are going to store about 10-20 elements - use vector. Especially for POD elements. I'm not sure about std::unordered_set
but map will give you a lot of memory allocations which will devour any speed boost from fast insert and erase. To speed up search you can sort vector and use some binary search ( lower_bound upper_bound equal_range ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.