Re: STL stack/deque
From: Leor Zolman (leor_at_bdsoft.com)
Date: 12/07/03
- Next message: Martijn Lievaart: "Re: STL stack/deque"
- Previous message: Greg Comeau: "Re: loading binary data"
- In reply to: osmium: "Re: STL stack/deque"
- Next in thread: Martijn Lievaart: "Re: STL stack/deque"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 07 Dec 2003 10:38:57 -0500
On Sun, 7 Dec 2003 08:32:46 -0800, "osmium" <r124c4u102@comcast.net>
wrote:
>Martijn Lievaart writes:
>
>> On Sun, 07 Dec 2003 06:44:10 -0800, osmium wrote:
>>
>> >> Vectors are optimised for random access with [] operator. Stacks use a
>> > deque
>> >> because it's more efficient. ( they can use some sort of linked list
>> >> arrangement, as opposed to a vector, which may well use a dynamically
>> >> re-allocated array so random access is fast )
>> >
>> > That sounds like idle speculation or rationalizing to me. It is hard to
>> > imagine anything more efficient that ix++ and ix--. Which is what you
>get
>> > when you model a stack using an array.
>>
>> What is the basic operation on a stack? Pushing and popping values. This
>> may require expansion of the container. For a vector this is an expensive
>> operation (the complete contents must be copied), while for dequeue it is
>> a fairly efficient operation (a new 'segment' is added).
>
>This is the kind of reasoning that would lead to an automobile with a 500
>gallon gas tank, and the commensurate reduction in gas mileage and
>acceleration. My word for that is "rationalizing". You do something and
>*then* you decide why you did it.
>
That's an interesting analogy, because I think it goes to further
support Martin's point (which I agree with). The C++ committee
characterizes the expected (required) performance of STL containers in
terms of performance guarantees, not arbitrary data structures (even
though the performance guarantees typically drive the choice of
underlying data structure.) That aspect of vectors that forces O(n)
performance when the vector has to grow is the problem; this problem
does not occur in deques. The irony is that a vector-based
implementation may actually run faster under certain circumstances
(based on how you use it), but then, you're of course free to override
the default container that stack is using.
As for the 500-gallon gas tank analogy: To me that's like using a
vector for a stack and reserving the 500 elements up front to avoid
the re-allocation problem! ;-)
-leor
Leor Zolman
BD Software
leor@bdsoft.com
www.bdsoft.com
C++ users: Download BD Software's free STL Error Message
Decryptor at: www.bdsoft.com/tools/stlfilt.html
- Next message: Martijn Lievaart: "Re: STL stack/deque"
- Previous message: Greg Comeau: "Re: loading binary data"
- In reply to: osmium: "Re: STL stack/deque"
- Next in thread: Martijn Lievaart: "Re: STL stack/deque"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|