473,395 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

In what circumstances should I choose to use deque over list?

Banfa
9,065 Expert Mod 8TB
So the basic sequenced C++ containers are

vector - holds data in a contiguous memory block that has the same memory footprint as a classical array. Expensive to insert or delete at the front or in the middle as it involves copying to maintain the memory layout, can be expensive to append to the end if it involves reallocating memory, never releases memory once allocated without user intervention. Has random access and therefore random access iterators. Should be the default container of choice unless a specific design decision can be found to use another container type.

deque - holds data in blocks of allocated memory, quick to insert or delete and beginning or end, deletion/insertion in the middle is slow. Has random access and therefore random access iterators.

list - holds data in individually allocated blocks of memory, quick to insert or delete and beginning, end or in the middle also quick to sort/reorder as these just involve swapping pointer values not objects. Does not have random access and therefore does not have random access iterators only sequential iterators.

The derived sequential contains, queue, priority_queue and stack are all implemented as adaptors of one of the basic sequential containers as follows

queue adapts deque
priority_queue adapts vector
stack adapts deque

Note that these adaptions are not fixed but are actually passed as template parameters.

So my question boils down to the queue adaption of deque.

queue interface member functions are

empty Test whether container is empty
size Return size
front Access next element
back Access last element
push Insert element implemented as push_back
pop Delete next element implemented as pop_front

In fact both deque and list have the correct members to be able to implement the queue adaptor. They also both seem to have good efficiency for the operations required. So the question is (finally after that long post :D )...

Why is the default queue implemented as an adaption of deque rather than an adaption of list? What is the design decision that makes deque the better choice in this case?
Nov 26 '09 #1
1 5550
weaknessforcats
9,208 Expert Mod 8TB
deque is a better choice because it allocates memory in chunks rather than allocating each time for one node. The standard templates are optimized for speed not size or efficiency.

Speed is paramount to the extent that PJ Plaugher once said that if you think you can write code that is faster than his templates, then think three times.

I would use list<> when I have a lot of nodes that are processing in a largely sequential manner and vector<> when the processing is more random in nature. Otherwise, I stick with deque.

The container-adapters (priority_queue, queue and stack) are just variants of the sequence containers so they are built on top of one of those containers (HAS-A).
Nov 26 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jenny | last post by:
Hi, I have a class foo which will construct some objects in my code. some of the objects store int values into the data deque, while others store float values to the deque. template <class...
19
by: chris | last post by:
Hello, I've recently been trying to understand the various structures supplied by c++, and the one I find most confusing is deque. One quick question about this. It seems most implementations...
11
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
9
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque...
5
by: Russell Warren | last post by:
Does anyone have an easier/faster/better way of popping from the middle of a deque than this? class mydeque(deque): def popmiddle(self, pos): self.rotate(-pos) ret = self.popleft()...
9
by: toton | last post by:
Hi, I am looking for a circular buffer solution ( a fixed buffer) , where the elements can be pushed back & removed front. It looks stl deque can be a solution. my question is, 1) circular...
1
by: Homeworkboy | last post by:
Can anyone help me with this program? Look at the bottom of this program for help methods: /*1. Make a program that uses numbers from 1 to 100 including each ends which puts the even...
12
by: arnuld | last post by:
It works fine. any advice on making it better or if I can improve my C++ coding skills: /* C++ Primer - 4/e * * Chapter 9 - Sequential Containers * exercise 9.18 - STATEMENT * ...
8
by: t | last post by:
The stack container adaptor uses deque as its default underlying sequential container. I don't understand why since we only add elements and remove elements from one side of a stack. Why isn't...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.