I need a sorted collection where elements can be modified. Is it safe to erase element after modification? Sorting key can be modified.
auto it=s.find(e)
modify(e)
s.erase(it)
I have made some tests in VS2010, and it worked. I think erase(it) does not need to search for element, so there is no need to call compare on element being erased.
It is hard to modify whole program to remove elements before modification, that is why I am looking for an alternative solution.
EDIT: adding working sample to make it more clear
#include <iostream>
#include <algorithm>
#include <set>
template <typename T>
struct PtrCmp
{
bool operator()(const T* x, const T* y) const
{
return *x<*y;
}
};
int main()
{
std::set<int*, PtrCmp<int>> aset;
int t[]={1,2,3,4};
for(int i=0;i<4;++i)
aset.insert(&t[i]);
auto it=aset.find(&t[2]);
t[2]=5;
aset.erase(it);
for(auto it=aset.begin(); it!=aset.end(); ++it)
std::cout<<**it<<std::endl;
}