c++ - Is it possible to erase modified element from std::set? -


i need sorted collection elements can modified. safe erase element after modification? sorting key can modified.

auto it=s.find(e) modify(e) s.erase(it) 

i have made tests in vs2010, , worked. think erase(it) not need search element, there no need call compare on element being erased.

it hard modify whole program remove elements before modification, why looking alternative solution.

edit: adding working sample make 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; } 

in example t[2]=5; modifies value used in comparator used e.g. rebalancing tree data structure behind set container. therefore such modification not safe because balanced-tree algorithm may fail in case factual key @ node changed, therefore not match expectation tree node. erase operation trigger tree rebalancing, , how undefined behavior. modification of value used in comparator factually break balancedness of tree behind set container.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -