How can we change unordered_multimap::local_iterator to unordered_multimap::iterator or unordered_multimap::const_iterator? I need the change because we cannot erase elements using local_iterator, and erasing can only be done using iterator/const_iterator. If there is any other way of erasing using local_iterator, please do suggest.
Is there a way to convert local_iterator to const_iterator or iterator?
137 Views Asked by Akhil Shukla At
2
There are 2 best solutions below
0

You could use find_if
algorithm to search for an element with the same address like so:
auto it = std::ranges::find_if(m,
[ptr = std::addressof(*local_it)](const auto& e) -> bool
{
return ptr == std::addressof(e);
}
);
Here is a full code snippet:
#include <string_view>
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <ranges>
#include <memory>
int main()
{
std::unordered_multimap<int, char> m;
m.insert({1, 'a'});
m.insert({1, 'b'});
m.insert({2, 'c'});
auto local_it = m.begin(m.bucket(1));
std::cout << local_it->first << local_it->second << '\n';
auto it = std::ranges::find_if(m,
[ptr = std::addressof(*local_it)](const auto& e) -> bool
{
return ptr == std::addressof(e);
}
);
std::cout << it->first << it->second << '\n';
return 0;
}
Run it here.
Kinda labor intensive, but you can iterate through the result of
equal_range()
until you find the correct iterator:As far as I can tell, that's as good as you can get currently.