简体   繁体   English

我应该使用哪种类型的容器存储ObjectID整数?

[英]What type of container should I use to store my ObjectID integer?

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. 我有GameObjects,它们存储在通用的GameObjectManager中。 These GameObjects have an ObjectID, which is a simple int. 这些GameObject具有一个ObjectID,它是一个简单的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. 我需要能够在基于2D切片的世界中插入或删除这些GameObject,其中每个TILE都包含一个ObjectID的容器。

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. 这样,我可以抓取特定的图块(例如Tile [10] [10]),然后通过从容器中读取内容来查看Tile [10] [10]上有哪些GameObject。 (ex. "Ah, so Character#4302 and Item#123 are on Tile[10][10]!") (例如,“啊,所以Character#4302和Item#123在Tile [10] [10]上!”)

Right now, each "Tile" is a structure in a MAP array of Tiles. 现在,每个“平铺”都是MAP的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. 但是,我读到,在任意添加/删除变量时不应使用Vector。 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. 相反,我将必须调用特定的ObjectID并将其从数组中的任何位置删除。

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. 也许我太累了,但是我可以对要使用的容器以及如何删除容器中的SPECIFIC变量使用一些建议。

For example, GameObject has an ObjectID of "422". 例如,GameObject的对象ID为“ 422”。 Tile[2][8] holds a container that has the following integers in order: 420, 421, 422, 433, 486, 800. Tile [2] [8]保存的容器按顺序具有以下整数:420、421、422、433、486、800。

I have a function: RemoveGameObjectFromMap(int ObjectID); 我有一个函数:RemoveGameObjectFromMap(int ObjectID); So I need it to remove 422, whenever I type (RemoveGameObjectFromMap(422); 因此,每当我键入(RemoveGameObjectFromMap(422);

I used to use a MAP container, but that is unnecessary. 我曾经使用过MAP容器,但这是不必要的。 Vector, according to cplusplus.com would be bad for this. 根据cplusplus.com的Vector,这样做是不利的。

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. 像每个图块有多少个ID(平均),插入,删除和查找的频率如何,有多少需求以最小化内存使用,以及其他100个注意事项。 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). 在这种情况下,显然可以替代std::vector方法是std::unordered_set (我假设您永远不会希望同一图块中的同一ID超过一​​次)。

If you are going to store about 10-20 elements - use vector. 如果要存储10-20个元素,请使用vector。 Especially for POD elements. 特别是对于POD元素。 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. 我不确定std::unordered_set但是map将为您提供大量内存分配,这将吞噬快速插入和擦除带来的任何速度提升。 To speed up search you can sort vector and use some binary search ( lower_bound upper_bound equal_range ). 为了加快搜索速度,您可以对向量进行排序,并使用一些二进制搜索( lower_bound upper_bound equal_range )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM