开发者

Build a queue of queues without using the STL

开发者 https://www.devze.com 2023-02-12 06:39 出处:网络
I\'m attempting to build a queue of queues that I can use to manage multiple data sets, but I\'m not sure how. Simply delaring queue<queue> doesn\'t work and returns the following error:

I'm attempting to build a queue of queues that I can use to manage multiple data sets, but I'm not sure how. Simply delaring queue<queue> doesn't work and returns the following error:

error: type/value mismatch at argument 1 in template parameter list for ‘template<class Type> class queue’
main.cpp:18: error:   expected a type, got ‘queue’
main.cpp:18: error: invalid type in declaration before ‘;’ token
make: *** [main.o] Error 1

Is it even possible to implement this sort of data str开发者_Python百科ucture, and if so, how should I go about it?

Note: This is a class assignment where we are required to code the queue ourselves rather than use the Standard Template Library.


A queue must have a type. You are creating a queue of queues of.... nothing. So it's reading the token queue inside of the < and > as an error, not a type. queue is not a type, technically. queue<int> is a type.

Do you want to write your own queue class or your own queue of queues class? What I'd advise is writing your own templated queue class... then instantiate that template with itself as the value type.

A queue of queues of ints? queue< queue<int> >. Whether that's the STL queue or your own queue.

NOTE: It's important that when you use a template type as the parameters of another template, you must put spaces around it. This is because the >> in queue<queue<int>> gets interpreted as the >> operator, due to the "Maximal Munch" rule in parsing. Completely unrelated note, writing the word queue that many times starts to mess with your eyes...


The particular error you're getting is that queue is a template class, so you need to specify an argument to it everywhere you use it. Thus writing

queue<queue>

Is illegal because the inner queue isn't paramterized over anything.

It's definitely possible to write your own queue of queues - in fact, it's possible to write any of the STL container classes - but starting off by using queue is not the right way to do this. If you want to make a queue of queues, start off by thinking about how to make a simple queue. What implementation would you use? How would you keep track of the number of elements? Would using a dynamic array work? How about a linked list, hash table, or binary search tree? Each structure has its pros and cons, so be sure to think it over before starting.

Once you have a good queue implementation, you can parameterize it to make your queue a template, from which you should easily be able to make the queue of queues.

Hope this helps, and good luck!


You need to define the type of the second queue, like queue<queue<int> > to have a queue of queues of integers.

0

精彩评论

暂无评论...
验证码 换一张
取 消