473,407 Members | 2,598 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,407 software developers and data experts.

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 6129

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.googlegr oups.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
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...
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...
3
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...
10
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...
9
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...
15
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...
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...
3
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.