c ++ push_back()在向量的映射内
我正在尝试动态添加元素到一个包含在地图中的向量,以存储映射到不同ID的多个粒子对象数组。我是新的语言,所以我有麻烦理解,如果这只能使用迭代器?在这种情况下,感觉像过度杀伤。是否可以直接访问地图中的向量?因为我可以通过键访问地图元素,并且因为每个键只有一个向量,它似乎应该是可能的。我真的没有确切的代码作为一个例子,但它看起来像这样:
I'm trying to dynamically add elements to a vector that is contained within a map, to store multiple arrays of "Particle" objects that are mapped to different ids. I'm new to the language and so I'm having trouble understanding if this can only be done with iterators? In this case it feels like overkill. Is it possible to directly access a vector inside a map? Since I can access the map elements by key, and because there is only one vector per key, it seems like it should be possible. I don't really have exact code as an example but it would look something like this:
int currentId = 1;
map <int, vector<Particle> > particleMap;
Particle p;
particleMap[currentId] <access to vector somehow here?> push_back(p);
我相信我在这里缺少一些更大的概念,但我发现自己需要这种类型数据结构很多,所以很难知道正确的方式来访问这些类型的表。
I'm sure I'm missing some larger concept here, but I find myself needing this type of data structure a lot, so it would be great to know the proper way to access these kinds of "tables."
particleMap[currentId].push_back(p);
会正常工作。
只有一个向量
每个id;这是你用 particleMap [currentId]
指的。然后你只需继续表达式,就像你在写 myVector.push_back(p)
。
There is only one vector
per id; this is what you are referring to with particleMap[currentId]
. Then you just continue with the expression as if you were writing myVector.push_back(p)
.