+ 1

Issue of pop back on vector of vector in c++

Hi I have below structure: structure str { string strNodeName; string strSheetName; Vector<vector<string> vecOneRowData; }; I have vector of above structure and what I am doing is adding object into vector if node Name is not present into vector... If object is already present, I am just adding vector of string into vecRowData. For specific case, I need to pop back data from vecRowData. This code works fine for smaller size of input. It fails and data randomly updates if pop back is performed more times.. I thought that after doing push_back post pop_back, issue occurs when push_back is asking for vector resize due to capacity reached. To avoid this, I thought to reserve 10000 elements for vector of vector.... It failed to compile and finally I did it but issue is not resolved. Commenting code line of pop_back works for any large input size. Unfortunately, I will not be able to share code for same as it contains confidential data. Feel free to ask in case of any query. Thanks in advance.

11th May 2020, 5:21 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 ответов
+ 1
pop_back deletes the last part and you are trying to expand the vector? For push-back you should be able to have a vector of any size, push_back and have it's size be one larger. You should not need to reserve a large number of elements. What may be happening (I don't know how large your vector is) is that you are out of continuous space. Vectors take a memory location and move on to the next and the next and the next until you are done or they hit something. You may want to use a linked list. I think there is one in std, I'm not 100% sure.
11th May 2020, 8:31 PM
Mark McGuire
Mark McGuire - avatar
0
I think a minimal example where we can reproduce the error would be helpful too. Maybe it's something else entirely. A vector is a few bytes big so an array of 10.000 vectors is maybe a quarter or half of a megabyte - not that much at all!
11th May 2020, 10:47 PM
Schindlabua
Schindlabua - avatar