【C++】仿函数(less)

C++中的仿函数

class Solution {
public:

    struct cmp
    {
        bool operator()(const pair&kv1,const pair&kv2)
        {
            if(kv1.secondkv2.first) return true;

            return false;
        }
    };

    vector topKFrequent(vector& words, int k) {
        mapmp;
        for(auto& w:words)
        {
            mp[w]++;
        }
        priority_queue,vector>,cmp>pq(mp.begin(),mp.end());
       
        vectorres;

        while(k--)
        {
            res.push_back(pq.top().first);
            pq.pop();
        }
        return res;
    }
};

对于以上代码一开始存在一点疑问,为什么自定义一下堆的排序规则还需要定义一个类、结构体然后再在类里面重写operator(),于是我跑去看了一下文档对priority_queue的定义。

【C++】仿函数(less)_第1张图片

嚯,比较器是一个类

【C++】仿函数(less)_第2张图片

这里的value_type是一个模板参数。

可以看到less<>当中没有具体的参数,typename显示的告诉编译器后面是一个类型,这里是告诉编译器,在Container这个类(容器)当中的value_type是一个类型。

less 是一个函数对象(即仿函数)或者函数模板,它不需要具体的变量名来执行比较操作。

<>用于指定类型,好让less的模板参数识别类型。

【C++】仿函数(less)_第3张图片

template 
  struct less : binary_function  {
  bool operator() (const T& x, const T& y) const {
      return x

再来看之前模拟实现的priority_queue代码,真的是边学边忘。

template,class Compare=std::less>
    class priority_queue
    {
    public:
        priority_queue()
        {}

        template
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                _con.push_back(*first);
                first++;
            }

            for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
            {
                Adjust_down(i);
            }
        }

        size_t size()
        {
            return _con.size();
        }

        void Adjust_up(size_t child)
        {
            Compare compare;
            size_t parent = (child - 1) / 2;
            while (child>0)
            {
                if (compare(_con[parent],_con[child]))
                {
                    std::swap(_con[parent], _con[child]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }

        }

        void Adjust_down(size_t parent)
        {
            Compare com;
            size_t child = parent * 2 + 1;
            while (child < _con.size())
            {
                // 选出左右孩子中大的那一个
                //if (child+1 < _con.size() && _con[child+1] > _con[child])
                //if (child + 1 < _con.size() && _con[child] < _con[child + 1])
                if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
                {
                    ++child;
                }

                //if (_con[child] > _con[parent])
                //if (_con[parent] < _con[child])
                if (com(_con[parent], _con[child]))
                {
                    std::swap(_con[child], _con[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }

        void push(const T& val)
        {
            _con.push_back(val);
            Adjust_up(_con.size()-1);//调堆
        }

        void pop()
        {
            std::swap(_con[0], _con[_con.size() - 1]);
            _con.pop_back();
            Adjust_down(0);
        }

        const T& top()const
        {
            return _con[0];
        }

        bool empty()
        {
            return (_con.size() == 0);
        }

        bool empty() const 
        {
            return (_con.size()==0);
        }

    private:
        Container _con;//容器
    };

将重点抠出来

template,class Compare=std::less>
void Adjust_down(size_t parent)
        {
            Compare com;//比较器
            size_t child = parent * 2 + 1;
            while (child < _con.size())
            {
                if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
                {
                    ++child;
                }
                
                if (com(_con[parent], _con[child]))
                {
                    std::swap(_con[child], _con[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }
Compare com;//比较器
com(_con[child], _con[child + 1]);//直接调用重载的运算符operator()

就是这样通过重载operator()来改变排序规则的。

总结:less是一个类、通过调用operator()来实现排序规则

你可能感兴趣的:(c++,less,开发语言,c,笔记)