Is there a way to convert local_iterator to const_iterator or iterator?

137 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

Kinda labor intensive, but you can iterate through the result of equal_range() until you find the correct iterator:

template<typename Cont>
typename Cont::iterator local_to_regular_iterator(Cont& c, typename Cont::local_iterator local_ite) {
  auto range = c.equal_range(local_ite->first);

  for(auto ite = range.first; ite != range.second; ++ite) {
    if(&ite->second == &local_ite->second) {
      return ite;
    }
  }

  throw std::out_of_range("huh?");
}

As far as I can tell, that's as good as you can get currently.

0
On

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.