Vector or Arrays
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.
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.
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.
But vectors (that are Absract Data Types ) are able to add new elements. They are dinamicly changing.
Ofcourse Vectors are much slower than arrays.
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
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; }
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement