+ 1
How vector of vector is managed
Hi I have vector of vector like vector<vector<int>> matrix(5); This means i have 5 vector of type vector<int>. Now i will be adding int to all of these 5 vector by doing push back Will these indiavidual vectors push change anything into outer vector memory layout ?
8 Réponses
+ 1
No problem with outer vector because allocation, internally, happen on heap and the vector maintains some refs about it... Anyway, why use a vector if the number of items is known and constant??
+ 1
Because it is not knwon to me at compile time... i know it before time of creation as size is output of a function which is not const expr
+ 1
Understood.. You can always use a dynamically allocated array 🙃
+ 1
My concern is when size and capacity of inner vector is same... now pushing new element will not change the base address of inner vector as it might not find new space contigously at same location
+ 1
Data of vectors (ref to internal data, capacity etc) are allocated contiguosly but the internal dal (pointed to ref) are allocated in other "memory zone".
In practice you change the layout of pointed internal data but notnof the vector
0
Yes thats correct...
But there will not be any push back on outer side as I am fine with 5 elements
There will be entries on inner vector using push back
0
Sharon Open new discussion about your problem and not post irrilevant questions to other discussions.
Futhermore add more problem details and code