Correct way to use "iterator absl::btree_map::insert(iterator, node_type&&)" ?

243 views
Skip to first unread message

Chris Burnley

unread,
Feb 14, 2022, 4:42:57 AM2/14/22
to Abseil.io
Hi, say I need to update the key of an entry in a btree_map. It's likely that the relative position doesn't change, so I would like to extract handle update the key and mapped value, then insert with a hint.

What is the correct way to do this?

I am doing something like this:

auto iter = map.find(key);
if (iter == map.end()) { .. }
auto hint = std::next(iter);
auto handle = map.extract(iter);
if (handle.empty()) return;
handle.key() = newKey;
handle.mapped() = newEntry;
return *map.insert(hint, std::move(handle));

On small test cases this works fine, but with production scenarios, I get this assertion failing on the insert:

absl/container/internal/btree.h:1489  assert(i <= finish());

Any ideas what I'm doing wrong here? I am using the "Mon Dec 20 08:44:32 2021 -0800" export. Typically it doesn't show up until quite a few modifications of the map, but have not pinpointed a small example yet.

Thanks, 
Chris
Reply all
Reply to author
Forward
0 new messages