Remove Elements from an Unordered Map Fulfilling a Predicate
Asked Answered
C

3

35

I want to remove elements (histogram bins) from an std::unordered_map (histogram) that fulfills a predictate (histogram bins having zero count) given as a lambda expression as follows

std::remove_if(begin(m_map), end(m_map), [](const Bin & bin) { return bin.second == 0; });

but GCC-4.6.1 complains as follows

/usr/include/c++/4.6/bits/stl_pair.h:156:2: error: assignment of read-only member ‘std::pair<const unsigned char, unsigned char>::first’
/usr/include/c++/4.6/bits/stl_pair.h: In member function ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_T1, _T2>&&) [with _T1 = const unsigned char, _T2 = long unsigned int, std::pair<_T1, _T2> = std::pair<const unsigned char, long unsigned int>]’:
/usr/include/c++/4.6/bits/stl_algo.h:1149:13:   instantiated from ‘_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = std::__detail::_Hashtable_iterator<std::pair<const unsigned char, long unsigned int>, false, false>, _Predicate = pnw::histogram<V, C, H>::pack() [with V = std::vector<unsigned char>, C = long unsigned int, H = std::unordered_map<unsigned char, long unsigned int, std::hash<unsigned char>, std::equal_to<unsigned char>, std::allocator<std::pair<const unsigned char, long unsigned int> > >]::<lambda(const Bin&)>]’
tests/../histogram.hpp:68:13:   instantiated from ‘void pnw::histogram<V, C, H>::pack() [with V = std::vector<unsigned char>, C = long unsigned int, H = std::unordered_map<unsigned char, long unsigned int, std::hash<unsigned char>, std::equal_to<unsigned char>, std::allocator<std::pair<const unsigned char, long unsigned int> > >]’
tests/../histogram.hpp:85:13:   instantiated from ‘void pnw::histogram<V, C, H>::normalize(uint) [with V = std::vector<unsigned char>, C = long unsigned int, H = std::unordered_map<unsigned char, long unsigned int, std::hash<unsigned char>, std::equal_to<unsigned char>, std::allocator<std::pair<const unsigned char, long unsigned int> > >, uint = unsigned int]’
tests/../histogram.hpp:121:51:   instantiated from ‘H& pnw::histogram<V, C, H>::add(It, It) [with It = __gnu_cxx::__normal_iterator<const unsigned char*, std::vector<unsigned char> >, V = std::vector<unsigned char>, C = long unsigned int, H = std::unordered_map<unsigned char, long unsigned int, std::hash<unsigned char>, std::equal_to<unsigned char>, std::allocator<std::pair<const unsigned char, long unsigned int> > >]’
tests/../histogram.hpp:129:55:   instantiated from ‘H& pnw::histogram<V, C, H>::add(const V&) [with V = std::vector<unsigned char>, C = long unsigned int, H = std::unordered_map<unsigned char, long unsigned int, std::hash<unsigned char>, std::equal_to<unsigned char>, std::allocator<std::pair<const unsigned char, long unsigned int> > >]’
tests/../histogram.hpp:57:60:   instantiated from ‘pnw::histogram<V, C, H>::histogram(const V&, pnw::histogram<V, C, H>::TYPE_t) [with V = std::vector<unsigned char>, C = long unsigned int, H = std::unordered_map<unsigned char, long unsigned int, std::hash<unsigned char>, std::equal_to<unsigned char>, std::allocator<std::pair<const unsigned char, long unsigned int> > >]’
tests/t_histogram.cpp:38:61:   instantiated from ‘void test_dense_histogram() [with T = unsigned char, C = long unsigned int]’
tests/t_histogram.cpp:64:5:   instantiated from ‘void test_histograms() [with C = long unsigned int]’
tests/t_histogram.cpp:200:29:   instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:156:2: error: assignment of read-only member ‘std::pair<const unsigned char, long unsigned int>::first’
make: *** [tests/t_histogram.o] Error 1

Isn't std::remove_if applicable to std::unordered_map?

Comrade answered 9/2, 2012 at 11:25 Comment(1)
I fell into this trap too, but now thinking about it std::remove (that moves elements to the end of the container) couldn't work with an associativity container. "the end of the container" doesn't really have meaning any more.Celle
M
54

The answer is no (you can't use remove_if on associative containers). You need to do a simple loop; the erase(iterator) member now returns the next valid iterator - so your loop becomes:

for(auto it = begin(m_map); it != end(m_map);)
{
  if (it->second == 0)
  {
    it = m_map.erase(it); // previously this was something like m_map.erase(it++);
  }
  else
    ++it;
}
Medalist answered 9/2, 2012 at 11:32 Comment(0)
P
7

There is a proposal of Uniform Container Erasure by Stephan T. Lavavej:

template <class K, class T, class H, class P, class A, class Predicate>
void erase_if(unordered_map<K, T, H, P, A>& c, Predicate pred);

i.e.

std::erase_if(m_map, [](const Bin& bin) { return bin.second == 0; });

Unfortunately it didn't make it C++17. I suppose this is tied to the adoption of the Ranges TS.

Phonogram answered 13/1, 2017 at 11:22 Comment(0)
B
3

Since c++20, it is possible to use erase_if on unordered_map.
It will return the number of removed elements.

From the cpp reference:

std::erase_if (std::unordered_map)
Erases all elements that satisfy the predicate pred from the container.

In your specific use case (remove all the elements where the second is 0) it would look like:

const auto count = std::erase_if(m_map, [](const auto& item) {
    auto const& [key, value] = item;
    return (value == 0);
});
Buckden answered 30/5, 2023 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.