C ++ push_back () inside a vector map

I am trying to dynamically add elements to a vector contained in a map to store several arrays of Particle objects that map to different identifiers. I am new to the language, and therefore I had trouble understanding, can this be done only with iterators? In this case, this seems redundant. Is it possible to get direct access to the vector inside the map? Since I can access the map elements by key, and because there is only one vector for each key, it seems that this should be possible. I actually don't have the exact code, but it looks 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 that some larger concept is missing here, but I really need a data structure of this type, so it would be great to know the correct way to access these types of โ€œtablesโ€.

+4
source share
1 answer
 particleMap[currentId].push_back(p); 

will work fine.

There is only one vector for each identifier; this is what you are talking about with particleMap[currentId] . Then you just continue the expression as if you were writing myVector.push_back(p) .

+15
source

Source: https://habr.com/ru/post/1343564/


All Articles