Reputation: 11
I am new to cpp STL I have a doubt regarding array of vectors
when we declare array of vectors like vectors <int> arr[100];
we can also say this as a 2-D matrix .But we if talk about 2-D arrays (int prr[100][100]
) then prr[0]
then it will print the address of first array in 0th row
Basically i want to ask why we are declaring vector <int> arr[100]
because arr[0]
will also store address of array at 0th array
we should declare like vector <int*> arr[100]
for declaring array of vectors
Upvotes: 1
Views: 468
Reputation: 206717
But we if talk about 2-D arrays (
int prr[100][100]
) thenprr[0]
then it will print the address of first array in 0th row
That is true to some extent and in some contexts.
prr[0]
is an object of type int [100]
-- an array of 100 int
s.
In some contexts, prr[0]
decays to a pointer to its first element, however it is not always true.
sizeof(prr[0])
will be 100 * sizeof(int)
.
&prr[0]
will be of type int (*)[100)
, pointer to "array of 100 int
s", not int**
.
In contrast,
Basically i want to ask why we are declaring
vector <int> arr[100]
becausearr[0]
will also store address of array at 0th array we should declare likevector <int*> arr[100]
for declaring array of vectors
No, arr[0]
will not store the address of the array at 0-th array. arr[0]
is of type std::vector<int>
. It's simple as that. Unlike an array, an object of type std::vector<int>
does not decay to a pointer to the first element of the object in any context. Hence any further comparisons with int*
does not make sense.
Upvotes: 3