Re: Vector or List

From: Hendrik Schober (SpamTrap_at_gmx.de)
Date: 08/23/04


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


Relevant Pages

  • Re: Accessing next/prev element while for looping
    ... > The python way is much more succinct. ... > previous or the next element in the array before continuing iterating. ... > incredibly silly given that python lists under the hood are linked ... the iterator used by the for loop, not the list it is iterating over. ...
    (comp.lang.python)
  • Re: Iterator Class?
    ... > there is a significant performace penalty when using getfor long Lists. ... > It appears as though getinsists on iterating from the beginning of the ... > list to arrive at the desired element. ... the profiler has never complained about iterating over a 10 ...
    (comp.lang.java.programmer)
  • Re: Iterator Class?
    ... > there is a significant performace penalty when using getfor long Lists. ... > It appears as though getinsists on iterating from the beginning of the ... > list to arrive at the desired element. ... the profiler has never complained about iterating over a 10 ...
    (comp.lang.java.help)
  • Re: variable declaration
    ... would forbid iterating through lists containing a mix of different types. ... you declare a variant type where the rules are relaxed *for that situation ... Thomas Bartkus ...
    (comp.lang.python)
  • Re: Calling one constructor from another in VC++
    ... >> redundant and at best a minor notational convenience. ... >> Classes had default argument lists for years before general overloading ... > That's the quote I remember, ... I'm Schobi at suespammers dot org ...
    (microsoft.public.dotnet.languages.vc)

Quantcast