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

Arrays over vectors?

P: n/a
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.

--
=========
Comp Whizz
=========
(The C++ beginner)
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Computer Whizz" <ol*********@hotmail.com> wrote in message
news:cl**********@newsg4.svr.pol.co.uk...
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
Jul 22 '05 #2

P: n/a
On Thu, 21 Oct 2004 08:38:07 +0100, "Computer Whizz"
<ol*********@hotmail.com> wrote:
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
Jul 22 '05 #3

P: n/a
In article <p5********************************@4ax.com>,
Tom Widmer <to********@hotmail.com> wrote:

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.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #4

P: n/a
Tom Widmer wrote:
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[].

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5

P: n/a

"Jon Bell" <jt*******@presby.edu> wrote in message
news:cl**********@jtbell.presby.edu...
In article <p5********************************@4ax.com>,
Tom Widmer <to********@hotmail.com> wrote:

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.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA


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 .

--
=========
Comp Whizz
=========
(The C++ beginner)
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.