Nicolas A. T.
Nicolas A. T.

Reputation: 173

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)

Upvotes: 0

Views: 6802

Answers (3)

hmjd
hmjd

Reputation: 121961

If your comiler supports C++11 features, an alternative to std::vector is std::array:

std::array<std::deque<f32>, 4> deques;

std::for_each(deques.begin(),
              deques.end(),
              [](const std::deque<f32>& a_d)
              {
                  std::cout << a_d.size() << "\n";
              });

If not, you can use std::vector as already stated. To create the std::vector with initial deque<f32> elements:

std::vector<std::deque<f32> > deques(4); // 4 empty deque<f32>s

for (std::vector<std::deque<f32> >::iterator i = deques.begin();
     i != deques.end();
     i++)
{
      std::cout << i->size() << "\n";
}

Upvotes: 2

Rook
Rook

Reputation: 6145

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.

Upvotes: 1

Andrew
Andrew

Reputation: 24846

std::vector<std::deque<f32>> array;

std::deque pos, vel, rot, prop;

array.push_back(pos);
array.push_back(vel);
array.push_back(rot);
array.push_back(prop);

Upvotes: 1

Related Questions