Search code examples
c++arraysdeque

How can I create an array of deques?


I have a lot of deques defined, and when I need to do things like erase or pop all of them, I've just had to do it to every deque specifically. What i thought could make it easier was to put the deques in an array or list of some kind, which I could loop through.

What I want to do is something like this (Basicly just pseudocode):

deque<f32> pos, vel, rot, prop;
deque deques[] = {pos, vel, rot, prop};
for(i=0; i<deques.length; i++) deques[i].pop_back();

(But it doesn't work)


Solution

  • Here you declare a simple unmanaged array:

    deque deques[] = {pos, vel, rot, prop}; 
    

    ...but you forget to declare the full specialised type of its contents, which should be deque<f32> not just a naked deque.

    Now, you try to iterate over your array,

    for(i=0; i<deques.length; i++) deques[i].pop_back(); 
    

    ...but simple C-style arrays don't have methods like length. You seem to be trying to write C#, not C++!

    Try this:

    std::array<std::deque<float>, 4> deques = { pos, vel, rot, prop };
    for(auto i=0; i<deques.size(); i++) deques[i].push_back(1.0f);
    

    etc.