Eliminar Elementos de un Mapa Desordenado Que cumple un Predicado


Quiero eliminar elementos (contenedores de histograma) de un std::unordered_map (histograma) que cumple con un predicto (contenedores de histograma con conteo cero) dado como una expresión lambda de la siguiente manera

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

Pero GCC-4.6.1 se queja de lo siguiente

/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

No es std::remove_if aplicable a std::unordered_map?

Author: Nordlöw, 2012-02-09

2 answers

La respuesta es no (no se puede usar remove_if en contenedores asociativos). Necesita hacer un bucle simple; el miembro erase(iterator) ahora devuelve el siguiente iterador válido, por lo que su bucle se convierte en:

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;
}
 31
Author: Nim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-09 11:32:06

Hay una propuesta de Borrado Uniforme del Contenedor por 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);

Es decir,

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

Desafortunadamente no lo hizo C++17. Supongo que esto está ligado a la adopción de los rangos TS.

 2
Author: akim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-13 13:21:33