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
The
newinstruction 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 usingnew. And the type on the right ofnewwill be the same type as the pointer type of the variable you’re initializing.In general, to dynamically allocate any type
X, you just writenew 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:Then you can write this:
If you don’t really need a pointer, then the declaration is simpler: