0
//try8.cpp
#include <utility>
#include <unordered_map>
#include <algorithm>


std::unordered_map<int, std::string> m_foo;

int main() {
    using value = std::unordered_map<int, std::string>::value_type;
    std::remove_if(m_foo.begin(), m_foo.end(), [](value& v){
            return v.first < 3;
        });
    return 0;
}

g++ ./try8.cpp gave me:

/usr/include/c++/7/bits/stl_algo.h:871:16: error: use of deleted function ‘std::pair<const int, std::__cxx11::basic_string<char> >& std::pair<const int, std::__cxx11::basic_string<char> >::operator=(const std::pair<const int, std::__cxx11::basic_string<char> >&)’
      *__result = _GLIBCXX_MOVE(*__first);
                ^
In file included from /usr/include/c++/7/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/7/bits/char_traits.h:39,
                 from /usr/include/c++/7/string:40,
                 from ./try8.cpp:1:
/usr/include/c++/7/bits/stl_pair.h:208:12: note: ‘std::pair<const int, std::__cxx11::basic_string<char> >& std::pair<const int, std::__cxx11::basic_string<char> >::operator=(const std::pair<const int, std::__cxx11::basic_string<char> >&)’ is implicitly declared as deleted because ‘std::pair<const int, std::__cxx11::basic_string<char> >’ declares a move constructor or move assignment operator
     struct pair

Can anybody explain this to me? Thanks! Why did my std::pair<int, std::string> pair become std::pair<*const* int, std::string>?

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
Long Bu
  • 513
  • 4
  • 9
  • 5
    `remove_if` changes the order of the container and you are not allowed to change the order of associative containers. – NathanOliver Sep 30 '20 at 14:17
  • 2
    "Why did my `std::pair` pair become `std::pair`?" It is not possible to change the key of an element in an associative container. The key is always `const`. See [`std::unordered_map::value_type`](https://en.cppreference.com/w/cpp/container/unordered_map). – François Andrieux Sep 30 '20 at 14:18
  • @FrançoisAndrieux thanks. – Long Bu Sep 30 '20 at 14:20

0 Answers0