Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ associative containers - why doesn't the standard defines methods to exchange and replaces keys?

I need to replace specific key values, while the rest of the value_type is left untouched. What I actually need to do, is copy the value, erase the entry and insert it with changed key value again. This is absolutely bad. I need to copy the whole value_type twice, and deallocate/allocate again.

Why the standard doesn't define methods like this:

// returns count of exchanged keys
size_type exchange_key(key_type const& x, key_type const& y);
// returns count of replaced keys
size_type replace_key(key_type const& old_key, key_type const& new_key);

Is there anything I'm missing?

like image 953
0xbadf00d Avatar asked Oct 21 '25 10:10

0xbadf00d


1 Answers

Now, you can, with .extract(key) (since C++17). https://en.cppreference.com/w/cpp/container/map/extract

like image 90
VTiTux Avatar answered Oct 25 '25 13:10

VTiTux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!