How vector of vector is managed | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 ?

6th Nov 2022, 7:33 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Answers
+ 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??
6th Nov 2022, 11:27 AM
KrOW
KrOW - avatar
+ 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
6th Nov 2022, 3:48 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Understood.. You can always use a dynamically allocated array 🙃
6th Nov 2022, 3:57 PM
KrOW
KrOW - avatar
+ 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
6th Nov 2022, 4:23 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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
6th Nov 2022, 4:36 PM
KrOW
KrOW - avatar
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
6th Nov 2022, 9:43 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Sharon Open new discussion about your problem and not post irrilevant questions to other discussions. Futhermore add more problem details and code
7th Nov 2022, 12:29 PM
KrOW
KrOW - avatar