I am iterating through a map which has the vector as the V parameter
map<SomeKey, vector<shared_ptr<SomeObject>>
I am iterating over to remove all instances of SomeObject
contained in the vectors in this map. I have currently tried a few approaches but each give me some sort of out of range error, or abort().
for (auto iterator = myMap.begin(); iterator != myMap.end(); ) {
shared_ptr<SomeKey> keyObject = iterator->first;
//reference the actual vector stored in the map, not a copy
vector<shared_ptr<SomeObject>> * someObjectList = &myMap[keyObject];
int index = findIndex(someObject, *someObjectList);
if (index != -1) {
auto itr = (*someObjectList).begin() + index;
&someObjectList->erase(itr);
}
//at this point, I do not want the SomeObject to exist in the vectors in the map,
//which is why I am trying to used a vector reference so it doesn't create a copy
vector<shared_ptr<SomeObject>> list = myMap[keyObject];
++iterator;
}
I have managed to get it working without using a referenced vector, and using a local copy, but it doesn't remove from the actual map vector, only the copied one in the current scope.
Can someone provide an example of how I can do this?
This is not a duplicate of the other post as I am not looping over the vector I want to remove elements from. I am looping over a map, that contains a vector.
The vectors are contained inside the map, and I am looking to created a reference vector that points to the one in the map, and remove elements directly from the map's vector.
map<SomeKey, vector<shared_ptr<SomeObject>> myMap;
//loop over map
for (auto it = myMap.begin(); it != myMap.end();) {
shared_ptr<K> keyObject = it->first;
vector<shared_ptr<V>> * referenceList = &myMap[keyObject];
referenceList.erase(myMap.begin() + 1);
//this would remove it from myMap[keyObject]
}
auto & clientList = connections[iterator->first];
for (auto i = clientList.begin(); i != clientList.end(); i++) {
if ((*i) == client) {
clientList.erase(i);//abort() thrown here
break;
}
}