You dont need to worry about memory leaks, in your case you use std::vector which uses RAII to make sure no memory is leaked. With STL containers you can “leak” in such case on;y if you do not shrink your vector after using remove algorithm.
Below is fix to your code, and not entire rewrite. This code removes all values equal to 1 [LIVE]:
vector<vector<int> > vec = { {0,1,2,3}, {0,1,2,3,4,5}, {0,1} };
vector<vector<int> >::iterator vit = vec.begin();
vector<int>::iterator it;
while(vit != vec.end()) {
it = vit->begin();
while(it != vit->end()) {
if( *it == 1 ) {
it = vit->erase(it);
// Not sure why you want this loop here
//while( condition )
//{
// **erase an element;**
//}
}
else {
++it;
}
}
++vit;
}
for (auto it1 = vec.begin(); it1 != vec.end(); ++it1){
for (auto it2 = it1->begin(); it2 != it1->end(); ++it2)
std::cout<<*it2<< ", ";
std::cout << "\n";
}
produces:
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
0, 2, 3,
0, 2, 3, 4, 5,
0,
3
solved How to erase elements in STL vector without copying iterator and without memory leakage?