【问题标题】:map deleter isn't working in C++?地图删除器在 C++ 中不起作用?
【发布时间】:2016-04-18 14:52:27
【问题描述】:

我在这里编写了一个自定义删除器来删除单个地图元素。但它不起作用。我知道我可以使用唯一指针解决这个问题。但我想知道如何在地图中做到这一点。

#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
class A 
{ 
    int i;
    public: 
    A() { }
    A(int pi = 0):i(pi)   {  cout<<"A()\n"; }
    void show() const { cout<<i<<endl; }
    ~A()   {  cout<<"~A()\n"; }
};
struct Deleter
{
    template <typename T>
    void operator () (T *ptr)
    {
         delete ptr;
    }
};
int main()
{
    map<char, A *> mymap;
    mymap['a'] =  new A(30);
    mymap['b'] =  new A(20);
    mymap['c'] =  new A(10);
    map<char, A *>::iterator it;
    for(it = mymap.begin(); it != mymap.end() ; it++)
        it->second->show();
    for_each(mymap.begin(),mymap.end(),Deleter());
    return 0;
}

显示编译时错误。

In file included from /usr/include/c++/4.9/algorithm:62:0,
                 from 4:
/usr/include/c++/4.9/bits/stl_algo.h: In instantiation of '_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = std::_Rb_tree_iterator<std::pair<const char, A*> >; _Funct = Deleter]':
32:49:   required from here
/usr/include/c++/4.9/bits/stl_algo.h:3755:14: error: no match for call to '(Deleter) (std::pair<const char, A*>&)'
  __f(*__first);
              ^
15:8: note: candidate is:
18:10: note: template<class T> void Deleter::operator()(T*)
18:10: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/algorithm:62:0,
                 from 4:
/usr/include/c++/4.9/bits/stl_algo.h:3755:14: note:   mismatched types 'T*' and 'std::pair<const char, A*>'
  __f(*__first);
              ^

【问题讨论】:

    标签: c++ dictionary stl


    【解决方案1】:

    正如错误消息所抱怨的那样,您尝试在std::pair 上使用delete,这根本不是一个指针。我猜你想deletestd::pair的第二个成员,即A*,你应该把Deleter改成:

    struct Deleter
    {
        template <typename K, typename V>
        void operator () (std::pair<K, V>& p)
        {
             delete p.second;
        }
    };
    

    LIVE

    【讨论】:

      【解决方案2】:

      Map 迭代器迭代键和值对。你必须重写你的删除器来接受这样的一对:

      struct Deleter {
          template<typename K, typename T>
          void operator() (std::pair<typename K, typename T> &p) {
             delete p.second;
          }
      }
      

      【讨论】:

        【解决方案3】:

        模板参数推导/替换失败:[...]
        注意:不匹配的类型 'T*' 和 'std::pair' __f(*__first);

        看起来足够了:你期待一个指针,但for_each给你一个,简单来说。

        __f(*__first);  // notice *
        

        所以制作:

        struct Deleter
        {
            template <typename T>
            void operator () (T pair)
            {
                 delete pair.second;
            }
        };
        

        【讨论】:

          【解决方案4】:

          删除器访问该对,所以你想要:

          struct Deleter
          {
              template <typename T>
              void operator () (T p)
              {
                   delete p.second;
              }
          };
          

          【讨论】:

            【解决方案5】:

            您假设 std::map::iterator::operator* 返回 值,但它实际上返回 std::pair&lt;const char,A*&gt;&amp;

            工作代码: http://coliru.stacked-crooked.com/a/e18bd4600575d39a

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-02-09
              • 2012-09-22
              • 1970-01-01
              • 2014-01-15
              • 1970-01-01
              • 2017-11-12
              • 2018-02-28
              • 1970-01-01
              相关资源
              最近更新 更多