Arrays over vectors?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Computer Whizz

    #1

    Arrays over vectors?

    I was reading through Accelerated C++ at work when I read through the first
    mention of Vectors, giving us certain functions etc.

    Is there any benefit of Arrays over Vectors? Since all Vectors seem to be
    (in my eyes at least) are glorified Arrays.

    - Now I know there's a bit more difference, but what exactly are the
    advantages of Arrays over Vectors (if any)?

    Oh, and please keep it in mind I am a real beginner in C++ but can
    understand a lot as I'm coming up from a background in other languages.

    --
    =========
    Comp Whizz
    =========
    (The C++ beginner)


  • John Harrison

    #2
    Re: Arrays over vectors?


    "Computer Whizz" <old486whizz@ho tmail.com> wrote in message
    news:cl7p50$sns $1@newsg4.svr.p ol.co.uk...[color=blue]
    > I was reading through Accelerated C++ at work when I read through the[/color]
    first[color=blue]
    > mention of Vectors, giving us certain functions etc.
    >
    > Is there any benefit of Arrays over Vectors? Since all Vectors seem to be
    > (in my eyes at least) are glorified Arrays.[/color]

    Arrays cannot be resized, but vectors can. Arrays cannot be copied but
    vectors can. vectors have a rich interface that is consistent with the rest
    of the standard template library, arrays have a much more limited interface.

    vectors use dynamic memory allocation to solve the limitations in C and C++
    with arrays and do so in a (relatively) safe manner.

    john


    Comment

    • Tom Widmer

      #3
      Re: Arrays over vectors?

      On Thu, 21 Oct 2004 08:38:07 +0100, "Computer Whizz"
      <old486whizz@ho tmail.com> wrote:
      [color=blue]
      >I was reading through Accelerated C++ at work when I read through the first
      >mention of Vectors, giving us certain functions etc.
      >
      >Is there any benefit of Arrays over Vectors? Since all Vectors seem to be
      >(in my eyes at least) are glorified Arrays.[/color]

      If you mean by an array:

      int array[100];

      then there is one benefit to arrays - performance. Creating the above
      array has essentially 0 runtime. By contrast:

      vector<int> v(100);

      has to make a memory allocation for the elements, and also 0
      initialises every element, both of which take time. You can use:

      boost::array<in t, 100> array;

      which has the same performance as the plain array, and offers extra
      features. See www.boost.org. Note the above will soon be
      std::tr1::array , since it is being added to the first standard library
      technical report.

      If by an array you mean:

      int* array = new int[size];

      then the vector is superior in almost every way, since it cleans up
      for you and provides many extra features for no major performance
      penalty (except for the potentially unnecessary initialization of
      every element, but that can be avoided by using push_back).

      Tom

      Comment

      • Jon Bell

        #4
        Re: Arrays over vectors?

        In article <p50fn0p2n8t33r jqbii8831ds69mf 88s3i@4ax.com>,
        Tom Widmer <tom_usenet@hot mail.com> wrote:[color=blue]
        >
        >If you mean by an array:
        >
        >int array[100];
        >
        >then there is one benefit to arrays - performance. Creating the above
        >array has essentially 0 runtime. By contrast:
        >
        >vector<int> v(100);
        >
        >has to make a memory allocation for the elements, and also 0
        >initialises every element, both of which take time.[/color]

        Whether this makes any practical difference depends of course on the
        particular program you're writing. I think it's safe to say that it
        matters only in some specialized cases where speed is of critical
        importance. It's not something that a beginner needs to worry about IMHO.
        He should stick to vectors at first, then learn arrays as a supplementary
        topic so as to be able to use them when they're really needed, or when
        he's working on someone else's project that does use them.

        --
        Jon Bell <jtbellm4h@pres by.edu> Presbyterian College
        Dept. of Physics and Computer Science Clinton, South Carolina USA

        Comment

        • Ioannis Vranos

          #5
          Re: Arrays over vectors?

          Tom Widmer wrote:
          [color=blue]
          > If you mean by an array:
          >
          > int array[100];
          >
          > then there is one benefit to arrays - performance. Creating the above
          > array has essentially 0 runtime. By contrast:
          >
          > vector<int> v(100);
          >
          > has to make a memory allocation for the elements, and also 0
          > initialises every element, both of which take time. You can use:
          >
          > boost::array<in t, 100> array;
          >
          > which has the same performance as the plain array, and offers extra
          > features. See www.boost.org. Note the above will soon be
          > std::tr1::array , since it is being added to the first standard library
          > technical report.[/color]


          For code under severe time and space constraints, the standard library
          also provides valarray.


          However in general, vector hasn't much efficiency difference than built
          in arrays when accessing elements via operator[].



          --
          Ioannis Vranos


          Comment

          • Computer Whizz

            #6
            Re: Arrays over vectors?


            "Jon Bell" <jtbellj3p@pres by.edu> wrote in message
            news:cl8act$qac $1@jtbell.presb y.edu...[color=blue]
            > In article <p50fn0p2n8t33r jqbii8831ds69mf 88s3i@4ax.com>,
            > Tom Widmer <tom_usenet@hot mail.com> wrote:[color=green]
            >>
            >>If you mean by an array:
            >>
            >>int array[100];
            >>
            >>then there is one benefit to arrays - performance. Creating the above
            >>array has essentially 0 runtime. By contrast:
            >>
            >>vector<int> v(100);
            >>
            >>has to make a memory allocation for the elements, and also 0
            >>initialises every element, both of which take time.[/color]
            >
            > Whether this makes any practical difference depends of course on the
            > particular program you're writing. I think it's safe to say that it
            > matters only in some specialized cases where speed is of critical
            > importance. It's not something that a beginner needs to worry about IMHO.
            > He should stick to vectors at first, then learn arrays as a supplementary
            > topic so as to be able to use them when they're really needed, or when
            > he's working on someone else's project that does use them.
            >
            > --
            > Jon Bell <jtbellm4h@pres by.edu> Presbyterian College
            > Dept. of Physics and Computer Science Clinton, South Carolina USA[/color]

            It's OK - I understand the idea behind arrays etc...

            So the speed is only a real issue with Large (or multi-dimensional)
            arrays/vectors... I'm guessing that vectors can be multi-dimensional here.

            I thought normal arrays could be resized also - but maybe the original
            contents would be over-written (as in VB and PHP - although there are
            functions to keep the values already in the array).

            Thank you for any further help - this newsgroup is introducing some
            interesting facts to me. I'm glad I'm keeping in the background and not
            making an ass of myself : D .

            --
            =========
            Comp Whizz
            =========
            (The C++ beginner)


            Comment

            Working...