Search code examples
c++dictionarydeque

delete elements from deque containing maps


I have a deque containing map and I am trying to delete an element by map key.

That was my try, but it is not working:

typedef map<string,string> mmap;
deque<mmap> q_map;



int main()
{
    mmap m;

    m.insert(std::make_pair("S180","11111111111"));

    q_map.push_back(m);
    std::cout << q_map.front().find("S180")->first << " " << q_map.front().find("S180")->second << std::endl;

    q_map.erase(std::remove(q_map.begin(), q_map.end(), q_map.front().find("S180")->first), q_map.end());
    std::cout << "=================================" << std::endl;


}

I am always getting this error:

error: cannot convert ‘std::_Deque_iterator<std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >*>’ to ‘const char*’ for argument ‘1’ to ‘int remove(const char*)’

Solution

  • You have two problems.

    First, which gives you the error message, is that you've likely forgotten to #include <algorithm> where std::remove is declared. Consequently you're accidentally trying to call a function with the same name but different parameters in <cstdio>.

    The second problem is that your call to std::remove does not match to the one in <algorithm> either. It's declared like this:

    template< class ForwardIt, class T >
    ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );
    

    And because the T of your deque iterator is mmap, you must pass a reference to mmap to remove all mmaps that have equal value. Instead you're passing q_map.front().find("S180")->first which is a std::string.