473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Vectors: not worthless? (out of curiosity)

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
6 1834

"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
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


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
12
by: Dave Theese | last post by:
Hello all, I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old...
56
by: john bailo | last post by:
I just installed mono from ximian on my redHat 9 workstation and wrote a simple program from the interesting book ado.net in c# by Mahesh Chand. mono is fun ( is there ado for linux ? ...
119
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see,...
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
3
by: John Bend | last post by:
Hello. Can anyone please suggest some good tutorial links where Java Vectors and Arrays are explained and compared? I'm a proficient programmer but fairly new to Java. Whilst I understand...
2
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
7
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>;...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.