Jump to content
  • Advertisement
Sign in to follow this  
Adam14

Vector or Arrays

This topic is 5093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!