The safest way to effectively change a set item that is used as a key
2 answers
Boost.MultiIndex allows in-place modification of elements, as the example shows:
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <cassert>
#include <set>
template<typename T>
using set_set=boost::multi_index_container<
std::set<T>,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<
boost::multi_index::identity<std::set<T>>
>
>
>;
int main()
{
set_set<int> ss={{0,1,2},{1,2,3},{2,3,4},{6,4,5}};
auto it=ss.find(std::set<int>{6,4,5}); // get last element
ss.modify(it,[](auto& s){
s.erase(6); // change 6
s.insert(3); // for 3
});
assert((*it==std::set<int>{3,4,5}));
}
+1
source to share
You can make the inner type not set
:
struct DerefComparator {
template <typename T>
bool operator()(const T& lhs, const T& rhs) const
{
return *lhs < *rhs;
}
};
std::set<std::shared_ptr<std::set<T>>, DerefComparator> the_set;
auto it = the_set.find(...);
// copy out - doesn't copy the whole set
auto inner = *it;
the_set.erase(it);
// make modifications as before
inner->insert(...);
inner->erase(...);
// move in
the_set.insert(std::move(inner));
+1
source to share