By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

Vectors: not worthless? (out of curiosity)

P: n/a
Jay
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
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Jay" <tw************@hotmail.com> wrote in message
news:Og****************@fe1.columbus.rr.com...
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
Jul 19 '05 #2

P: n/a
On Mon, 28 Jul 2003 10:53:34 GMT, "Jay" <tw************@hotmail.com>
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
Jul 19 '05 #3

P: n/a


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
kb******@gascad.at
Jul 19 '05 #4

P: n/a
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, ar*@acm.org
Jul 19 '05 #5

P: n/a
Jay

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
Jul 19 '05 #6

P: n/a
John Harrison wrote:
"Jay" <tw************@hotmail.com> wrote in message
news:7p*****************@fe3.columbus.rr.com... <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.)

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.