Arrays over vectors?

C

Computer Whizz

I was reading through Accelerated C++ at work when I read through the first
mention of Vectors, giving us certain functions etc.

Is there any benefit of Arrays over Vectors? Since all Vectors seem to be
(in my eyes at least) are glorified Arrays.

- Now I know there's a bit more difference, but what exactly are the
advantages of Arrays over Vectors (if any)?

Oh, and please keep it in mind I am a real beginner in C++ but can
understand a lot as I'm coming up from a background in other languages.
 
J

John Harrison

Computer Whizz said:
I was reading through Accelerated C++ at work when I read through the first
mention of Vectors, giving us certain functions etc.

Is there any benefit of Arrays over Vectors? Since all Vectors seem to be
(in my eyes at least) are glorified Arrays.

Arrays cannot be resized, but vectors can. Arrays cannot be copied but
vectors can. vectors have a rich interface that is consistent with the rest
of the standard template library, arrays have a much more limited interface.

vectors use dynamic memory allocation to solve the limitations in C and C++
with arrays and do so in a (relatively) safe manner.

john
 
T

Tom Widmer

I was reading through Accelerated C++ at work when I read through the first
mention of Vectors, giving us certain functions etc.

Is there any benefit of Arrays over Vectors? Since all Vectors seem to be
(in my eyes at least) are glorified Arrays.

If you mean by an array:

int array[100];

then there is one benefit to arrays - performance. Creating the above
array has essentially 0 runtime. By contrast:

vector<int> v(100);

has to make a memory allocation for the elements, and also 0
initialises every element, both of which take time. You can use:

boost::array<int, 100> array;

which has the same performance as the plain array, and offers extra
features. See www.boost.org. Note the above will soon be
std::tr1::array, since it is being added to the first standard library
technical report.

If by an array you mean:

int* array = new int[size];

then the vector is superior in almost every way, since it cleans up
for you and provides many extra features for no major performance
penalty (except for the potentially unnecessary initialization of
every element, but that can be avoided by using push_back).

Tom
 
J

Jon Bell

If you mean by an array:

int array[100];

then there is one benefit to arrays - performance. Creating the above
array has essentially 0 runtime. By contrast:

vector<int> v(100);

has to make a memory allocation for the elements, and also 0
initialises every element, both of which take time.

Whether this makes any practical difference depends of course on the
particular program you're writing. I think it's safe to say that it
matters only in some specialized cases where speed is of critical
importance. It's not something that a beginner needs to worry about IMHO.
He should stick to vectors at first, then learn arrays as a supplementary
topic so as to be able to use them when they're really needed, or when
he's working on someone else's project that does use them.
 
I

Ioannis Vranos

Tom said:
If you mean by an array:

int array[100];

then there is one benefit to arrays - performance. Creating the above
array has essentially 0 runtime. By contrast:

vector<int> v(100);

has to make a memory allocation for the elements, and also 0
initialises every element, both of which take time. You can use:

boost::array<int, 100> array;

which has the same performance as the plain array, and offers extra
features. See www.boost.org. Note the above will soon be
std::tr1::array, since it is being added to the first standard library
technical report.


For code under severe time and space constraints, the standard library
also provides valarray.


However in general, vector hasn't much efficiency difference than built
in arrays when accessing elements via operator[].
 
C

Computer Whizz

Jon Bell said:
If you mean by an array:

int array[100];

then there is one benefit to arrays - performance. Creating the above
array has essentially 0 runtime. By contrast:

vector<int> v(100);

has to make a memory allocation for the elements, and also 0
initialises every element, both of which take time.

Whether this makes any practical difference depends of course on the
particular program you're writing. I think it's safe to say that it
matters only in some specialized cases where speed is of critical
importance. It's not something that a beginner needs to worry about IMHO.
He should stick to vectors at first, then learn arrays as a supplementary
topic so as to be able to use them when they're really needed, or when
he's working on someone else's project that does use them.

It's OK - I understand the idea behind arrays etc...

So the speed is only a real issue with Large (or multi-dimensional)
arrays/vectors... I'm guessing that vectors can be multi-dimensional here.

I thought normal arrays could be resized also - but maybe the original
contents would be over-written (as in VB and PHP - although there are
functions to keep the values already in the array).

Thank you for any further help - this newsgroup is introducing some
interesting facts to me. I'm glad I'm keeping in the background and not
making an ass of myself : D .
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top