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
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;
Why
Code:
vector<int> a[10];
cout<<a[2];//Valid
cin>>a[2];//Invalide
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:
Where one should use array and where vector.
...
What is vector equivalent of apart from using iterators.
std::vector provides a one-dimensional array, but it is possible to use it to implement multi-dimensional arrays.Quote:
Is it possible to have 2D,3D...,ND vectors.
The above code declares a static array of 10 vectors of ints. This means that a[2] is a vector of ints.Code:vector<int> a[10];
which declares a vector of vectors of ints.Code:or vector< vector<int> > ivec;
Instead try:Quote:
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!!
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);
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)
if
is an array then why I am not able to use it likeCode:vector<int> a[10];
Code:a[2]=10;
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.
So... when you declare a vector like so,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 ... */
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.Code:vector<int> myV(10);
When you subsequentely type,
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)Code:myV.push_back(3);
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
Array of 3 vector?Code:vector<int> arr[3], is an array of 3 vectors (vectors of integers)
Can you elaborate on this
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.
ugh! I'll try :)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.
Oh! Now I have realised that
is actually a array of pointer pointing to 10 vectors of size 0.Code:vector<int> a[10];
Here is proof
Ok.,So, this way we can make a 2D vector.Code:vector<int> a[5];
a[0].push_back(5);
cout<<a[0][0];
Wee! :)
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 :)