Re: Vector or List
From: Hendrik Schober (SpamTrap_at_gmx.de)
Date: 08/23/04
- Next message: tom_usenet: "Re: Bug Report: char_traits<char>::compare() dose different."
- Previous message: Macca: "Vector or List"
- In reply to: Macca: "Vector or List"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 23 Aug 2004 12:35:49 +0200
Macca <Macca@discussions.microsoft.com> wrote:
> Hi,
>
> I'm fairly new to the STL and i was wondering when was the best situations
> to use a vector over a list and vice versa? and what are the advantages and
> disadvantages of each method.
Basically, vector shines when you need to
access elements randomly, but inserting or
removing elements anywhere else than at the
end is expensive. (Appending might also
trigger re-allocation once in a while, which
causes all objects to be copied.)
With list, the latter is cheap, but access
to elements other than iterating over them
in the given order (or backwards) is not
possible (as it does not provide random
access iterators).
Bottomline: The ideal use-case for a vector
is a container that is built once (preferably
with knowledge of how many objects are going
to be added) and accessed often. For lists,
it's when random insertions/removal are to be
expected and iteration is the only way the
elements are accessed.
(BTW, a compromise of both is the deque.)
> Thanks
HTH,
> Macca
Schobi
-- SpamTrap@gmx.de is never read I'm Schobi at suespammers dot org "Sometimes compilers are so much more reasonable than people." Scott Meyers
- Next message: tom_usenet: "Re: Bug Report: char_traits<char>::compare() dose different."
- Previous message: Macca: "Vector or List"
- In reply to: Macca: "Vector or List"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|