473,769 Members | 7,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

should we prefer deque over vector

after reading http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp,

i realize that deque has its own speed advantage over vector in all the
aspect (except for memory deallocation). does it mean that we should
prefer deque over vector? (in contrast with c++ standard, which
recommence vector over deque)

thanks!

Sep 14 '06 #1
5 6157

ya************@ gmail.com wrote:
after reading http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp,

i realize that deque has its own speed advantage over vector in all the
aspect (except for memory deallocation). does it mean that we should
prefer deque over vector? (in contrast with c++ standard, which
recommence vector over deque)
deque is better if you are going to do inserts or removes at both ends.
vector is better is better if you only insert and remove at the back.

Sep 14 '06 #2

Earl Purple wrote:
ya************@ gmail.com wrote:
after reading http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp,

i realize that deque has its own speed advantage over vector in all the
aspect (except for memory deallocation). does it mean that we should
prefer deque over vector? (in contrast with c++ standard, which
recommence vector over deque)

deque is better if you are going to do inserts or removes at both ends.
Also much nicer behaviour in case capacity needs to change and lower
memory requirements.
vector is better is better if you only insert and remove at the back.
In what way? You mean to say that indexing is faster?

Sep 14 '06 #3

Earl Purple wrote:
ya************@ gmail.com wrote:
after reading http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp,

i realize that deque has its own speed advantage over vector in all the
aspect (except for memory deallocation). does it mean that we should
prefer deque over vector? (in contrast with c++ standard, which
recommence vector over deque)

deque is better if you are going to do inserts or removes at both ends.
Also much nicer behaviour in case capacity needs to change and lower
memory requirements.
vector is better is better if you only insert and remove at the back.
In what way? You mean to say that indexing is faster?
Peter

Sep 14 '06 #4

Earl Purple wrote:
ya************@ gmail.com wrote:
after reading http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp,

i realize that deque has its own speed advantage over vector in all the
aspect (except for memory deallocation). does it mean that we should
prefer deque over vector? (in contrast with c++ standard, which
recommence vector over deque)

deque is better if you are going to do inserts or removes at both ends.
Also much nicer behaviour in case capacity needs to change and lower
memory requirements.
vector is better is better if you only insert and remove at the back.
In what way? You mean to say that indexing is faster?

/Peter

Sep 14 '06 #5
<ya************ @gmail.comwrote in message
news:11******** **************@ k70g2000cwa.goo glegroups.com.. .
: after reading http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp,
:
: i realize that deque has its own speed advantage over vector in all
the
: aspect (except for memory deallocation).

When reading that article, you should pay attention to two things:
- its focus is really on collection creation and disposal.
- it studies collections of hundreds of thousands of items.
- it says nothing about how the code was compiled: were
optimizations enabled? I expect that std::vector will
perform better in optimized mode than std::deque.
[ this will be especially true if the author used
the debug-enabled standard library of Visual Studio ]

For small object collection, std::vector is just simpler.
It inter-operates more easily with legacy code or libraries
that expect contiguous arrays of data. Contiguous storage
also tends to provide better performance when processing
collections of numeric data.

std::deque might waste large amounts of memory when it stores
a small collection (some implementations may allocate chunks of
hundreds of items at a time, even when only 1 element is stored).
Iterators for std::deque typically cause a performance overhead
(both in space and time).

Maybe more importantly, any insert/erase anywhere in a deque
(even push/pop at either end) will invalidate all iterators.
std::vector is more controllable and predictable in terms
of iterator invalidation.
: does it mean that we should
: prefer deque over vector? (in contrast with c++ standard, which
: recommence vector over deque)

The real point is: you should care in the first place for the
operations that are supported by the container, not for "speed"
is some random benchmark. Most C++ developers still tend to think
in terms of arrays when they use sequence containers, and
std::vector is the simplest container that fulfills this need.

In practice, I only use std::deque:
- when I need to implement a queue / fifo buffer
(that's what deque is best at).
- for stack-like buffers that I know will be very large,
and when testing has demonstrated that deque does
indeed tend to perform better than std::vector.
- possibly as well want push_back to never invalidate
item references (this is guaranteed by deque if only
push/pop_front/back are used).

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Sep 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
4380
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 of deque are quite complex. Why couldn't I implement deque with a couple of vectors V,W where the deque is the element of V in reverse order followed by W? This would appear to me to satisfy all the conditions, and be significantly simpler. Am I...
9
3734
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 depends on the size of its members even if the deque is empty? is there at all a way to check out how much memory my deque occupies? i've read that the sizeof operator cannot be used with dynamically allocated arrays so i figured it wouldn't give...
3
7990
by: Peter Ammon | last post by:
According to SGI's STL reference, a deque: Supports amortized constant time random access (like a vector) Allows amortized constant time insertions at the beginning and end One way you could implement this would be to allocate a chunk of memory and start in the middle rather than in the beginning, but then deque would have a reserve() function. So how is deque usually implemented? Thanks,
10
3228
by: Whybother | last post by:
I have this typedef map<__int64, int> Results_Map; __int64 is defined as 8 bytes ranging from -9,223,372,036.854,775,808 to 9,223,372,036.854,775,807 I have loaded it with approx 34 million hash keys and when the program is shutting down it takes forever (10+ mins) while its deleting the objects. The memory it uses according to task manager is varies from 500,000 k then
9
3673
by: Ian | last post by:
I would like to port my class library written in standard C++ with STL to C++/CLI. Several of my classes inherit from std::vector or std::deque. From what I can see, dot.net does not offer equivalent classes. Is this really the case or have I missed an entire section of the dot.net documentation? One could argue ArrayList offers a poor substitute to std::vector. I understand an effort was made to port STL to C++/CLI and this would...
15
3535
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
8
3695
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 the default vector instead? For that matter, why not list since we don't access elements in the middle of the stack? I don't know if it's necessary (and don't see that it is) to do so in the implementation of stack. Separate question: vector...
3
1655
by: escholtz | last post by:
Dear All, I am trying to use the STL-'find' function to find all the locations of a specific repeating element in an unsorted vector/deque. I want the original vector/deque to stay unsorted. It is my understanding from the text books that 'find' will only return the location of the first occurence of the element in question. Is there an existing STL function that I can use? Any suggestions would be greatly be appreciated. Cheers...
1
5599
Banfa
by: Banfa | last post by:
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...
0
10214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.