I have this map where keys are strings and values are vectors of Product type (structs containing and int "code" variable and some other variables). Given a code, I want to remove all occurrences of vector elements where that code is present, and I need to do this for all map elements. This is what I'm trying and my program crashes:
void removeByCode(map<string, vector<Product>> &products, int code){
for (auto productlist: products){
for (auto i=productlist.second.begin(); i != productlist.second.end(); i++){
if ((*i).code==code)
i = productlist.second.erase(i);
}
}
}
This is one use case where it crashes (assume this is what the map pairs look like):
a: [code:1; code:2]
b: [code:1]
c: [code:1; code:2]
Code to be erased: 2
PS: I need to use erase (not remove or other methods)