Reputation: 131
Since, I'm new to this language so, I'm having hard time in understanding these differences.
what is the difference between the two?
vector<int> *ad;
and
vector<int*> ad;
also, how are these two lines equivalent?
vector<int> * ad = new vector<int>[5];
and
vector<int> ad[5];
Upvotes: 1
Views: 53
Reputation: 1266
vector<int> *ad;
declares ad as a pointer, the type of ad is such that it can be assigned to point to a vector of integers. Those integers are held by-value in the vector. The vector "owns" them and controls the lifetime of the integers. Because ad has not been assigned it doesn't actually point to such a vector (yet)/
vector<int*> ad;
declares ad as a vector of pointers which can point to integers. The vector owns the pointers, they have not been assigned to point to any specific integers. This time the vector does actually exist, but its empty.
vector<int> * ad = new vector<int>[5];
declares ad as pointer to a vector of integers and assigns it to point to the first element of a new array of 5 vector of integers. That array of vectors is put on the heap, it will continue to exist until it is deleted.
vector<int> ad[5];
declare ad as an array of 5 vectors of integers. ad will exist until it goes out of scope. The vectors will be empty.
Upvotes: 3