Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector.<> vs array

What are the pros and contras of using a Vector.<> instead of array?

like image 614
Ilya Boyandin Avatar asked Jul 15 '09 09:07

Ilya Boyandin


People also ask

Is the difference between an array and a vector?

Vector is a sequential container to store elements and not index based. Array stores a fixed-size sequential collection of elements of the same type and it is index based. Vector is dynamic in nature so, size increases with insertion of elements. As array is fixed size, once initialized can't be resized.

Why do we use vector instead of array?

Vector are implemented as dynamic arrays with list interface whereas arrays can be implemented as statically or dynamically with primitive data type interface. Size of arrays are fixed whereas the vectors are resizable i.e they can grow and shrink as vectors are allocated on heap memory.

Are vectors always better than arrays?

There is a myth that for run-time speed, one should use arrays. A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program.


2 Answers

From the adobe documentation page:

As a result of its restrictions, a Vector has two primary benefits over an Array instance whose elements are all instances of a single class:

  • Performance: array element access and iteration are much faster when using a Vector instance than when using an Array.
  • Type safety: in strict mode the compiler can identify data type errors such as assigning a value of the incorrect data type to a Vector or expecting the wrong data type when reading a value from a Vector. Note, however, that when using the push() method or unshift() method to add values to a Vector, the arguments' data types are not checked at compile time but are checked at run time.
like image 168
Arno Avatar answered Sep 23 '22 07:09

Arno


Pro: Vector is faster than Array - e.g. see this: Faster JPEG Encoding with Flash Player 10

Contra: Vector requires FP10, and according to http://riastats.com/ some 20% of users are still using FP9

like image 33
tst Avatar answered Sep 20 '22 07:09

tst