Sign in to follow this  
Adam14

Vector or Arrays

Recommended Posts

Chris Hare    462
Microsoft Developer Network ==> a lot of information about programming etc.

array ==> statically sized block of equal typed elements.
vector ==> dynamically sized block of equal typed elements.

A vector is essentially an array that grows and shrinks. STL provides std::vector.

Share this post


Link to post
Share on other sites
Fen    882
array has a specified size, so You cannot resize it and add new elements. fe: int array[100]; array[200] = 60;
But vectors (that are Absract Data Types ) are able to add new elements. They are dinamicly changing.

Ofcourse Vectors are much slower than arrays.

Share this post


Link to post
Share on other sites
risingdragon3    382
Quote:


Ofcourse Vectors are much slower than arrays.


Depends. And still, you should qualify "slower". We shouldn't spread any FUD around in the beginner's forum. Arrays are not resizeable *, if they are declared like this:

int happy[100];

As well, you must know before your program runs exactly how big the array must be. So of course this is a problem if you need to hold anything that can change at "runtime", ie when the program actually runs.

So we have vectors. Vectors are like arrays, but they have a key difference, that is vectors are resizeable. They can change size dynamically at "runtime". There is a cost associated with this. But you cannot say vectors are "slower" then arrays, since arrays cannot resize (and thus cannot incur that cost).
However, you should realize, that vectors are "almost" arrays. When you access some element of an vector, it is exactly as fast as accessing an array.

There are many implementations of vectors around. One standard one is the stl::vector, which is a template. Using it is simple, something like this:


#include <vector>
...

std::vector<int> happy;
happy.push_back(1);
happy.push_back(2);
happy.push_back(3);
cout << happy[0] << " " << happy[1] << " " << happy[2]; //prints 1 2 3


If you use the stl::vector, you will find that it's "almost" like using an array.

-----------------

* not including pointers. I'm purposely avoiding that complication.

Share this post


Link to post
Share on other sites
eleusive    300
Another big payoff of using the std::vector class is that you can use the std algorithms with them. These can be very usefull, such as 1 line sorting, copying, transformations, permutations ect

Share this post


Link to post
Share on other sites
smart_idiot    1298
You can use algorithms on normal arrays. Example:

#include <iostream>
#include <algorithm>

int main()
{
int array[5] = {7, 6, 1, 3, 9};
int *ptr = std::find(array, array+5, 9);

if(ptr == array+5)
std::cout << "Array doesn't seem to contain a 9." << std::endl;
else
std::cout << "9 is element " << ptr-array+1 << "." << std::endl;

return 0;
}

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by Fen
Ofcourse Vectors are much slower than arrays.


Care to back that argument?


#include <iostream>
#include <vector>
#include <windows.h>

#pragma comment(lib, "winmm.lib")

const size_t n_iter = 10000;
const size_t array_size = 100000;

int main(int argc, char* argv[])
{
std::vector<int> array(array_size);
// int array[array_size];

DWORD start = timeGetTime();
for(size_t iteration=0; iteration<n_iter; ++iteration)
for(size_t index = 0; index<array_size; ++index)
++array[index];
DWORD finish = timeGetTime();

std::cout << << finish - start << " ms" << std::endl;
}


10000 elements array - 10000 iterations : 10715ms
10000 elements vector - 10000 iterations : 10705ms

The difference is completely insignificant.

Share this post


Link to post
Share on other sites
The only way the vector is slower is when allocating, becuase it uses dynamic instead of static storage and if that overhead somehow affects performance in a big way you're (probably) doing something very strange and should be rethinking your approach.

Share this post


Link to post
Share on other sites
smart_idiot    1298
Quote:
Original post by Fruny
10000 elements array - 10000 iterations : 10715ms
10000 elements vector - 10000 iterations : 10705ms

The difference is completely insignificant.


Not only that, the vector managed to beat the array anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by smart_idiot
Quote:
Original post by Fruny
10000 elements array - 10000 iterations : 10715ms
10000 elements vector - 10000 iterations : 10705ms

The difference is completely insignificant.


Not only that, the vector managed to beat the array anyway.


We cannot say that vectore managed to beat array because of the resolution of timeGetTime() timer... The resolution can be five ms or more(Win2000 and XP), so the real results could be differnt...

But - the difference beetween result is insignificant itself

Share this post


Link to post
Share on other sites
python_regious    929
I profiled a vector and array test program once, to compare the assembly outputs and the speed. In the end, I found that on average ( over arbitary large number ) that vectors were around 99% to 100% of the speed of arrays. We're talking a few cycles here, which is sod all. This is timing accesses btw.

[Edited by - python_regious on July 8, 2004 7:04:57 AM]

Share this post


Link to post
Share on other sites
CC Ricers    1491
So basically vectors are like arrays, only that you can "push" and "pop" items in them? I just got that from learning server-side scripting languages.

Share this post


Link to post
Share on other sites
Telastyn    3777
To me, a vector is a data type which has a position and a directional magnitude. [used for speed, force and the such in physics] That the STL variable length array structure is named the same thing is unfortunate.

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by CC Ricers
So basically vectors are like arrays, only that you can "push" and "pop" items in them? I just got that from learning server-side scripting languages.


You can do a bit more than just 'push' and 'pop' (read the docs for the complete details of what operations are available), but that's the idea, yes.

Quote:
Original post by Telastyn
To me, a vector is a data type which has a position and a directional magnitude. [used for speed, force and the such in physics] That the STL variable length array structure is named the same thing is unfortunate.


In the field of parallel processing, arrays of numbers which are processed .. in parallel are called vectors. This is where the name comes from. Ironically, C++ does have a standard numeric array class (valarray), which allows you to write, say a*=2 to multiply all the elements in the array by two, thus being closer to the parallel processing concept of vector. If anything it is that one that should have rightfully be named vector and the dynamic array class be named something like array.

Share this post


Link to post
Share on other sites
Chris Hare    462
Quote:
Original post by Telastyn
To me, a vector is a data type which has a position and a directional magnitude. [used for speed, force and the such in physics] That the STL variable length array structure is named the same thing is unfortunate.


I come from a math background, and have too always associated a mathematical nature with the name 'vector.' I wrote my own vector type, using a namespace.

Also, a static array/vector comparison is invalid. vector is not used in the place of a static array, but a dynamic array where many insertions are made/required, and it is here that the speed and safety of vector is victorius.

Share this post


Link to post
Share on other sites
Wildfire    154
Quote:
IronWolf
A vector is like an array, however vectors give you more control.


When you think about it, std::vector is just a wrapper around a dynamic array. So how is it supposed to offer you 'more control' then direct access does? I'd say 'more convenience & safety'.

Share this post


Link to post
Share on other sites
kdogg    204
Basically, there are a bunch of functions that you frequently call on arrays. You will frequently have to resize them, or sort them, or copy them, or add to the middle of them, and so on. A vector is a collection of routines that implement all those functions in manner which is very efficient for average usage. A vector is essentially an array bundled with all these functions so you don't have to write them yourself. So far as performing any of these operations, the vector will most likely perform just as well as your own functions. However, you might be able to get a slight performance gain in realistic use (both in speed and memory usage) by writing your own class since you'll know the nature of your data and might find some optimizations based on that knowledge that a vector will not implement. This performance boost is usually minimal and the average programmer would be well advised to save time and use vectors unless they find that the vectors are consuming more resources than is acceptable.

With that said, i use arrays for most things since most of my data doesn't require the functionality of a vector anyways.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this