 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jay Guest
|
Posted: Mon Jul 28, 2003 10:53 am Post subject: 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
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Mon Jul 28, 2003 11:02 am Post subject: Re: Vectors: not worthless? (out of curiosity) |
|
|
"Jay" <twentycavities (AT) hotmail (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
tom_usenet Guest
|
Posted: Mon Jul 28, 2003 1:45 pm Post subject: Re: Vectors: not worthless? (out of curiosity) |
|
|
On Mon, 28 Jul 2003 10:53:34 GMT, "Jay" <twentycavities (AT) hotmail (DOT) com>
wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Mon Jul 28, 2003 2:06 pm Post subject: Re: Vectors: not worthless? (out of curiosity) |
|
|
Jay wrote:
| Quote: |
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
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Mon Jul 28, 2003 2:49 pm Post subject: 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, [email]ark (AT) acm (DOT) org[/email]
|
|
| Back to top |
|
 |
Jay Guest
|
Posted: Tue Jul 29, 2003 4:06 am Post subject: 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
|
|
| Back to top |
|
 |
Adam Fineman Guest
|
Posted: Tue Jul 29, 2003 1:48 pm Post subject: Re: Vectors: not worthless? (out of curiosity) |
|
|
John Harrison wrote:
| Quote: | "Jay" <twentycavities (AT) hotmail (DOT) com> wrote in message
news:7pmVa.19370$hc.5742 (AT) fe3 (DOT) 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
cout << 'n';
sort(array, array + ARRAY);
cout << "After: ";
copy(array, array + ARRAY, ostream_iterator
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.)
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|