Vector or Arrays

Started by
18 comments, last by kdogg 19 years, 9 months ago
What exactly is the difference between Vector or Arrays? Thanks.
I wanna make a game.
Advertisement
MSDN is your friend, you know...
If at first you don't succeed, redefine success.
uhh this feels noobish but what is MSDN?
I wanna make a game.
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.
msdn just in case you had't found it
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.

Se my programming blog: Code and Graphics

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.
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
"What are you trying to tell me? That I can write an O(N^2) recursive solution for a 2-dimensional knapsack?" "No, programmer. I'm trying to tell you that when you're ready, you won't have to." -Adapted from "The Matrix"
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; }
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement