# Array v/s Vector

• 06-07-2006
vaibhav
Array v/s Vector
I have recently learnt vectors. Where one should use array and where vector. Is it possible to have 2D,3D...,ND vectors. What is vector equivalent of apart from using iterators.

Code:

```int a[10]; a[2]=5;```
• 06-07-2006
vaibhav
Why

Code:

``` vector<int> a[10]; cout<<a[2];//Valid cin>>a[2];//Invalide```
• 06-07-2006
laserlight
Quote:

Where one should use array and where vector.
...
What is vector equivalent of apart from using iterators.
A std::vector is a dynamic array, so one would use it when a dynamic array is needed. If a fixed size array is needed, one could use std::tr1::array (if it is available). From what I see, there will generally be no need to use the C style arrays. Of course, the most obvious exceptions would be in the implementation of these advanced constructs (e.g., std::vector and std::tr1::array).

Quote:

Is it possible to have 2D,3D...,ND vectors.
std::vector provides a one-dimensional array, but it is possible to use it to implement multi-dimensional arrays.

Code:

`vector<int> a[10];`
The above code declares a static array of 10 vectors of ints. This means that a[2] is a vector of ints.
• 06-07-2006
Mario F.
Code:

`or vector< vector<int> > ivec;`
which declares a vector of vectors of ints.
• 06-07-2006
Darryl
Quote:

Originally Posted by vaibhav
Why

Code:

``` vector<int> a[10]; cout<<a[2];//Valid cin>>a[2];//Invalide```

Code:

```vector <int> a(10); //declare a vector of 10 ints (with default value of 0) cout<<a[2];//Valid cin>>a[2];//Valid now!!```
• 06-08-2006
vaibhav
Wow now,that's work
But what's the difference between

Code:

```vector<int> a[10];  //Even other functions like size and push_back does not seem to work //and vector<int> a(10);```
• 06-08-2006
Mario F.
The first declares an array and the second declares a vector. That is why the first cannot be used with push_back.

Remember array declarations?
type arr[size] , where size is optional

The square brackets (known as subscript operators) determine the variable is an array. And type determines what type of array your variable is. So,

int arr[3] is an array of 3 integers

and

vector<int> arr[3], is an array of 3 vectors (vectors of integers)
• 06-08-2006
vaibhav
if
Code:

`vector<int> a[10];`
is an array then why I am not able to use it like
Code:

`a[2]=10;`
• 06-08-2006
Mario F.
Hmm... there's another thing in your question that worried me. It is important to understand what the vector member function push_back() does.

It inserts a value into the vector. That is, it increments the size of your vector by 1 and places a new value in that new position.

Code:

```vector<int> myV; myV.push_back(12);  //myV has 1 element. cout << myV[0];        //outputs 12 myV.push_back(7)    //myV has now 2 elements cout << myV[1]          //outputs 7 /*... and so forth ... */```
So... when you declare a vector like so,
Code:

`vector<int> myV(10);`
You are declaring a vector of 10 elements. If declared inside a function (and main() is also a function), these elements are uninitialized. Which means they contain whatever garbage was on that location in memory.

When you subsequentely type,
Code:

`myV.push_back(3);`
You will be making your vector grow to 11 elements and the value 3 will be placed on this new position. (In arrays and containers, the back of an array is the last element, the front of an array is the first element)

So... I personally prefer my vectors to be declared without any elements (and empty vector just like I did on the first piece of code up there) and then use a loop to populate them with push_back(). I also hear this is best, since populating a vector with already defined dimensions is slower.

However, there are ways to initialize vectors with defined dimensions.

Code:

```vector<int> myV(10, 0) //Will create a vector of 10 elements, each initialized to 0 vector<int> myV(anotherV)  // Will create a vector that is an exact copy of the anotherV vector```
• 06-08-2006
vaibhav
Code:

`vector<int> arr[3], is an array of 3 vectors (vectors of integers)`
Array of 3 vector?
Can you elaborate on this
• 06-08-2006
Mario F.
Quote:

Originally Posted by vaibhav
if
Code:

`vector<int> a[10];`
is an array then why I am not able to use it like
Code:

`a[2]=10;`

Because it is an array alright. But an array of vectors.

According to my last post, if you now look closely at your declaration you will suddenly realize you declare an array of vectors, but you didn't define how many elements each vector will have. Right now your array points to a vector without any dimensions.
• 06-08-2006
Mario F.
Quote:

Originally Posted by vaibhav
Code:

`vector<int> arr[3], is an array of 3 vectors (vectors of integers)`
Array of 3 vector?
Can you elaborate on this

ugh! I'll try :)

vector<int> is the type of your array.

First... arrays are compound types. A compound type is a type that is defined in terms of another type.

So... first look at your variable name... arr. arr is your variable.

Next look at your square brackets. They say your variable is of type array.

Inside the brackets you placed 3. So, your variable that is an array has 3 dimensions. In short, your variable is an array of 3 elements

Next we need to know what type these elements are. The type for all variable definitions is the first thing on the line. on this case, vector<int>. So, your variable is an array named arr, of 3 elements, in which each element is a vector of integers

Now... look at simpler array:

int arr[3];

Your variable is an array named arr, in which each element is an integer.

Now look at a vector:

vector<int> arr(3); // note the parenthesis.

Your variable is a vector of 3 elements, in which each element is an integer.
• 06-08-2006
vaibhav
Oh! Now I have realised that
Code:

`vector<int> a[10];`
is actually a array of pointer pointing to 10 vectors of size 0.

Here is proof
Code:

```vector<int> a[5]; a[0].push_back(5); cout<<a[0][0];```
Ok.,So, this way we can make a 2D vector.
• 06-08-2006
Mario F.
Wee! :)
• 06-08-2006
Mario F.
I don't want to confuse you much more. But has to be said... you didn't create a 2D vector, but you did create a 2D container (or matrix).

Regardless, you got the idea. And that for now should suffice :)