I have an std::vector instance as defined in the following line:
std::vector< std::pair<EndPointAddr*, EndPointAddr*>* > mServiceSubscriptionsList;
The first item in the underlying std::pair object is the network address of the subscriber entity while the second is the network address of the subscribed entity. So, an std::pair object represents a subscription here as pair of subscriber and subscribed endpoint addresses.
I would like to delete all subscriptions in this vector for a given subscriber endpoint address. For this purpose, I wrote below indicated function where I use std::remove_if with a predicate. Based on the documentation of std::remove_if, my understanding is that std::remove_if puts all occurrences to be deleted to the end of the vector and moves the end of the vector backward to its new position.
My questions is:
How can I reach to these std::pair items that are put into the end of the vector after the call to remove_if in order to deallocate their contents dynamically one by one (i.e. dellocating std::pair* pointers)? Could you indicate the needed code snippet in the below function code? I can delete the first occurence kept in the iterator last. However, I am not sure how can I delete the rest of the occurences. Thanks.
bool
XXX::removeSubscriptionForASpecificSubscriber(EndPointAddr * ptrSubscriberAddr)
{
auto last =
std::remove_if(mServiceSubscriptionsList.begin(),
mServiceSubscriptionsList.end(),
[ptrSubscriberAddr](std::pair<EndPointAddr*, EndPointAddr*>* thePair)
{
return ptrSubscriberAddr->getXXXAddress().compareTo(thePair->first->getXXXAddress());
});
if(last != mServiceSubscriptionsList.end())
{
//HERE I CAN DELET THE FIRST OCCURENCE, but WHAT I WANT IS TO DELETE ALL OCCURANCES
if(*last != nullptr)
{
delete *last;
}
mServiceSubscriptionsList.erase(last, mServiceSubscriptionsList.end());
return true;
}
return false;
}