Vectors: not worthless? (out of curiosity)

J

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
 
J

John Harrison

Jay said:
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
 
T

tom_usenet

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
 
K

Karl Heinz Buchegger

Jay said:
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.
 
A

Andrew Koenig

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?
 
J

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
 
A

Adam Fineman

John said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top