Nosrettap
Nosrettap

Reputation: 11320

How do I dynamically create a queue with an underlying data structure of list in c++?

I want to create a dynamic queue (using the keyword new) with an underlying data structure of list in c++ but I cannot figure out the syntax for it. What I have so far is:

queue<int, list<int>> myQueue = new queue<int, 

but I cannot figure out what to finish this line with. Can someone help me? Thanks

Upvotes: 0

Views: 360

Answers (1)

Rob Kennedy
Rob Kennedy

Reputation: 163277

The new instruction returns a pointer, so you don't finish that line at all. You need the variable type to be a pointer if you insist on using new. And the type on the right of new will be the same type as the pointer type of the variable you're initializing.

queue<int, list<int> >* myQueue = new queue<int, list<int> >;

In general, to dynamically allocate any type X, you just write new X. Perhaps you were a little confused because of how complicated the full name of your type is (commas, angle brackets, multiple tokens, etc.). You can simplify it with a typedef to give the name a single-token name:

typedef queue<int, list<int> > int_list_queue;

Then you can write this:

int_list_queue* myQueue = new int_list_queue;

If you don't really need a pointer, then the declaration is simpler:

queue<int, list<int> > myQueue;
// or
int_list_queue myQueue;

Upvotes: 6

Related Questions