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;

Printable View

- 06-07-2006vaibhavArray 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-2006vaibhav
Why

Code:

vector<int> a[10];

cout<<a[2];//Valid

cin>>a[2];//Invalide

- 06-07-2006laserlightQuote:

Where one should use array and where vector.

...

What is vector equivalent of apart from using iterators.

Quote:

Is it possible to have 2D,3D...,ND vectors.

Code:`vector<int> a[10];`

- 06-07-2006Mario F.Code:
`or vector< vector<int> > ivec;`

- 06-07-2006DarrylQuote:

Originally Posted by**vaibhav**

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-2006vaibhav
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-2006Mario 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?

, where*type*arr[*size*]*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-2006vaibhav
if

Code:`vector<int> a[10];`

Code:`a[2]=10;`

- 06-08-2006Mario 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 ... */

Code:`vector<int> myV(10);`

When you subsequentely type,

Code:`myV.push_back(3);`

*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-2006vaibhavCode:
`vector<int> arr[3], is an array of 3 vectors (vectors of integers)`

Can you elaborate on this - 06-08-2006Mario F.Quote:

Originally Posted by**vaibhav**

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-2006Mario F.Quote:

Originally Posted by**vaibhav**

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-2006vaibhav
Oh! Now I have realised that

Code:`vector<int> a[10];`

Here is proof

Code:`vector<int> a[5];`

a[0].push_back(5);

cout<<a[0][0];

- 06-08-2006Mario F.
Wee! :)

- 06-08-2006Mario 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 :)