Vectors: not worthless? (out of curiosity)

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

    Vectors: not worthless? (out of curiosity)

    Hi,
    I'm a C++="C with classes" kind of guy and was wondering if anyone felt like
    making the case that STL Vectors are in any way better than my home-brewed,
    memmove()-heavy dynamic array class (ignoring the it's-a-standard point).


    Thanks,

    Jay


  • John Harrison

    #2
    Re: Vectors: not worthless? (out of curiosity)


    "Jay" <twentycavities @hotmail.com> wrote in message
    news:Og7Va.5689 $IQ2.320@fe1.co lumbus.rr.com.. .[color=blue]
    > Hi,
    > I'm a C++="C with classes" kind of guy and was wondering if anyone felt[/color]
    like[color=blue]
    > making the case that STL Vectors are in any way better than my[/color]
    home-brewed,[color=blue]
    > memmove()-heavy dynamic array class (ignoring the it's-a-standard point).
    >
    >
    > Thanks,
    >
    > Jay
    >[/color]

    Well the obvious problem with your home brewed classes don't work with types
    that cannot be memmove'd (i.e. pretty much any user defined type).

    Without knowing anything more about your home brewed class its hard to
    comment. But

    1) Does it use iterators (incompatible with the STL algorithms if not)
    2) Does it provide any exception safety guarantees
    3) Does it have as good an interface as the STL vector
    4) Does it allow custom allocators

    john


    Comment

    • tom_usenet

      #3
      Re: Vectors: not worthless? (out of curiosity)

      On Mon, 28 Jul 2003 10:53:34 GMT, "Jay" <twentycavities @hotmail.com>
      wrote:
      [color=blue]
      >Hi,
      >I'm a C++="C with classes" kind of guy and was wondering if anyone felt like
      >making the case that STL Vectors are in any way better than my home-brewed,
      >memmove()-heavy dynamic array class (ignoring the it's-a-standard point).[/color]

      That really depends on your dynamic array class (which I assume is a
      class template rather than a class?). Is your use of memmove correct
      (or at least portable) for user defined types? Post your class if you
      want a critique, and the relative advantages/disadvantages of vector
      pointed out.

      Tom

      Comment

      • Karl Heinz Buchegger

        #4
        Re: Vectors: not worthless? (out of curiosity)



        Jay wrote:[color=blue]
        >
        > Hi,
        > I'm a C++="C with classes" kind of guy and was wondering if anyone felt like
        > making the case that STL Vectors are in any way better than my home-brewed,
        > memmove()-heavy dynamic array class (ignoring the it's-a-standard point).
        >[/color]

        std::vector does not use memmove and thus is able to handle any
        user defined classes, while your class is not.
        Also: post your class, especially the reallocation, the copy
        constructor and the assignment operator and somebody might find
        some problems in this.

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Andrew Koenig

          #5
          Re: Vectors: not worthless? (out of curiosity)

          Jay> I'm a C++="C with classes" kind of guy and was wondering if
          Jay> anyone felt like making the case that STL Vectors are in any way
          Jay> better than my home-brewed, memmove()-heavy dynamic array class
          Jay> (ignoring the it's-a-standard point).

          You mean you want people to volunteer to convince you that your
          work is inferior to someone else's? When you would be the sole
          judge of the inferiority? Why bother?

          --
          Andrew Koenig, ark@acm.org

          Comment

          • Jay

            #6
            Re: Vectors: not worthless? (out of curiosity)


            Hey,
            Thanks very much for all the responses. To clear some things up:
            * By "I'm a C with classes kind of guy" I mean I'm pretty happy with C and a
            few select C++ features (e.g. classes, templates, exceptions).
            * My question was: Why would anyone use a dynamic array class with such an
            awkward interface (IMHO!)? How much more efficient than my (or any other
            simple) memmove()-based thing could it be?
            * The answer seems to be: For my simple purposes, Vector is useless.
            However, there are all sorts of features I'm not even aware of, let alone
            exploiting (such as uber-efficient STL algorithms, as John pointed out). If
            I ever need to sort a dynamic array, I'll make it a Vector.
            * I'd post my actual home-brewed class, but I'm allergic to ridicule (heh).
            For user-defined types, I just use an array of pointers. The day I implement
            "custom allocators", etc. is the day after I find a use for such things.



            Thanks again,

            Jay


            Comment

            • Adam Fineman

              #7
              Re: Vectors: not worthless? (out of curiosity)

              John Harrison wrote:[color=blue]
              > "Jay" <twentycavities @hotmail.com> wrote in message
              > news:7pmVa.1937 0$hc.5742@fe3.c olumbus.rr.com. ..[/color]
              <snip>[color=blue]
              > If
              >[color=green]
              >>I ever need to sort a dynamic array, I'll make it a Vector.[/color]
              >
              >
              > Actually you don't need to, all you need to do is modify your own array
              > class so that it uses iterators.[/color]

              Or even with a C-style array:

              -----------------
              #include <algorithm>
              #include <vector>
              #include <iostream>
              #include <iterator>

              using namespace std;

              int
              main()
              {
              const unsigned int ARRAY = 5;

              int array[ARRAY] = { 5, 2, 7, 9, 1 };

              cout << "Before: ";
              copy(array, array + ARRAY, ostream_iterato r<int>(cout, " "));
              cout << '\n';

              sort(array, array + ARRAY);

              cout << "After: ";
              copy(array, array + ARRAY, ostream_iterato r<int>(cout, " "));
              cout << '\n';

              return 0;
              }
              ---------------

              However, that's not really the point. The best reason to not reinvent
              the wheel in this case is that new people coming to the project will
              already know how to use a vector (or you shouldn't have hired them). No
              one will know how to use your home-grown array class, and they will
              waste time not only trying to learn it, but will spend time wondering
              why the designer felt it necessary to roll his own.

              If you bear in mind that you should be writing your code for the next
              person who has to look at it, I think your choice is clear.

              --
              Adam Fineman

              (Reverse domain name to reply.)

              Comment

              Working...