c ++ push_back()在向量的映射内 [英] c++ push_back() inside a map of vectors

查看:120
本文介绍了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).

这篇关于c ++ push_back()在向量的映射内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆