Vectors: not worthless? (out of curiosity)

Discussion in 'C++' started by Jay, Jul 28, 2003.

  1. Jay

    Jay Guest

    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
     
    Jay, Jul 28, 2003
    #1
    1. Advertising

  2. "Jay" <> wrote in message
    news:Og7Va.5689$...
    > 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
    >


    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
     
    John Harrison, Jul 28, 2003
    #2
    1. Advertising

  3. Jay

    tom_usenet Guest

    On Mon, 28 Jul 2003 10:53:34 GMT, "Jay" <>
    wrote:

    >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).


    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
     
    tom_usenet, Jul 28, 2003
    #3
  4. Jay wrote:
    >
    > 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).
    >


    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
     
    Karl Heinz Buchegger, Jul 28, 2003
    #4
  5. 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,
     
    Andrew Koenig, Jul 28, 2003
    #5
  6. Jay

    Jay Guest

    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
     
    Jay, Jul 29, 2003
    #6
  7. Jay

    Adam Fineman Guest

    John Harrison wrote:
    > "Jay" <> wrote in message
    > news:7pmVa.19370$...

    <snip>
    > If
    >
    >>I ever need to sort a dynamic array, I'll make it a Vector.

    >
    >
    > Actually you don't need to, all you need to do is modify your own array
    > class so that it uses iterators.


    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_iterator<int>(cout, " "));
    cout << '\n';

    sort(array, array + ARRAY);

    cout << "After: ";
    copy(array, array + ARRAY, ostream_iterator<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.)
     
    Adam Fineman, Jul 29, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cm9kY2hhcg==?=

    out of curiosity

    =?Utf-8?B?cm9kY2hhcg==?=, Sep 9, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    901
    =?Utf-8?B?cm9kY2hhcg==?=
    Sep 12, 2005
  2. Larry Hastings
    Replies:
    2
    Views:
    292
    Istvan Albert
    Sep 14, 2004
  3. Replies:
    3
    Views:
    257
    Alex Martelli
    Jun 19, 2006
  4. Replies:
    3
    Views:
    702
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    447
    Guest
    Sep 14, 2005
Loading...

Share This Page