I have an implementation which can be done by either std::queue
or std::list
.
Does it make a difference which one I use. std::queue
seems to have less functions so it is lighter than std::list
?
-
2It depends entirely on what you are doing... – Yuushi Feb 13 '13 at 01:36
-
2See [How can I efficiently select a Standard Library container in C++11?](http://stackoverflow.com/questions/10699265/how-can-i-efficiently-select-a-standard-library-container-in-c11) – sehe Feb 13 '13 at 01:38
-
@Yuushi if a specific implementation can be done with ::push or ::pop, which is lighter to use , or does it not matter ? And what do you mean by what I am doing ? In what regards ? If I am eating a banana, is queue lighter then ?? – navderm Feb 13 '13 at 01:38
-
@sgar91 : with all due respect, why are u spending time putting quotes on posts ? Any specific advantages to it ? – navderm Feb 13 '13 at 01:40
-
@navderm I mean that without telling us anything at all about your problem, what kind of access patterns you expect, or posting any code at all, your question is meaningless. Extra member functions don't make a data structure "heavier" (whatever that means). – Yuushi Feb 13 '13 at 01:43
-
@navderm You should format every part of your question, so when a person takes a quick glance, each part of the question is clearly visible. – sgarizvi Feb 13 '13 at 01:46
-
@sgar91 : ok i'll try doing that. – navderm Feb 13 '13 at 01:47
-
@Yuushi : so you're essentially wasting your time answering a meaningless question. hope you're having fun doing it. – navderm Feb 13 '13 at 01:48
2 Answers
Does it make a difference which one I use?
Yes - in several ways. The primary consideration is readability: by using an std::deque
you convey to the readers of your code your intention to insert and delete only at the ends, and not in the middle, of the container. This is a very nice thing to know when you read someone else's code.
A secondary consideration is the implementation of the container itself: unlike lists, which allocate and store their elements individually, std::deque
stores its elements in chunks to save space. This may reduce the footprint of the container, and/or make it slightly faster. On the flip side, there are potential inefficiencies when it comes to iterating through the entire queue.
The efficiency considerations, however, should not be a factor during your initial design, because it is a matter of optimization.
I think that the best approach is to strive for best readability: use the container that conveys your intentions to the readers of your code in the cleanest possible way.

- 714,442
- 84
- 1,110
- 1,523
seems to have less functions so it is lighter ...?
More functions does not make an object heavier weight. I would use the collection that makes the most sense in your scenario. If you'll always be accessing this in a First-In-First-Out manner, then std::queue
would be appropriate. If you need double-linked list functionality, use std::list
.

- 554,122
- 78
- 1,158
- 1,373