Question:
The update operation in std::set seems cumbersome due to the lack of an in-place modification API. Currently, users resort to finding the element, copying it, updating its value, erasing the original, and reinserting the updated copy. Is there a more efficient approach or should the std::set be overridden with a custom implementation?
Answer:
std::set employs const iterators to maintain its ordered nature. Allowing regular iterators would enable changing item values and potentially disrupting the ordering.
Hence, the idiomatic method to update items in a set is the one you described:
Customizing std::set by creating your own implementation is unnecessary since the current approach is considered the standard way to perform updates in this container.
The above is the detailed content of Is There a More Efficient Way to Update Elements in a std::set?. For more information, please follow other related articles on the PHP Chinese website!