从包含地图的双端队列中删除元素

时间:2015-03-05 17:32:43

标签: c++ dictionary deque

我有一个包含地图的双端队列,我正在尝试按地图键删除元素。

这是我的尝试,但它不起作用:

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;


}

我总是收到这个错误:

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*)’

1 个答案:

答案 0 :(得分:0)

你有两个问题。

首先,它给出了错误消息,是您可能忘记#include <algorithm>声明std::remove的地方。因此,您无意中尝试在<cstdio>中调用具有相同名称但参数不同的函数。

第二个问题是,您对std::remove的呼叫与<algorithm>中的呼叫不匹配。它的声明如下:

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

因为你的deque迭代器的Tmmap,你必须传递一个mmap的引用来删除所有具有相同值的mmaps。相反,您传递的q_map.front().find("S180")->firststd::string