【高级程序设计语言C++】仿函数

文章介绍了一种使用C++仿写优先级队列的方法,该队列基于vector容器构建。默认实现为小根堆,通过调整代码中的比较操作符,可以实现大根堆。当不支持修改源代码时,文章提出使用仿函数(如Less和Greater类)来改变比较逻辑,从而轻松切换堆的类型。
摘要由CSDN通过智能技术生成

先来看一段仿写优先级队列的代码

template <class T, class Container = vector<T>>
class priority_queue
{
public:
    priority_queue()
    {}
    
    void Adjust_Down(size_t parent)
    {
        size_t child = parent * 2 + 1;
        while (child < c.size())
        {
            if (child + 1 < c.size() && c[child+1] > c[child])
            {
                child++;               
            }
            //if (c[child] < c[parent])
            if(cmop(c[child],c[parent]))
            {
                swap(c[child], c[parent]);
                parent = child;
                child = parent * 2 + 1;
            }
            else
            {
                break;
            }
        }
    }

    void Adjust_Up(size_t child)
    {
        size_t parent = (child - 1) / 2;
        while (child > 0)
        {
            if (c[child] < c[parent])
            {
                swap(c[child], c[parent]);
                child = parent;
                parent = (child - 1) / 2;
            }
            else
            {
                break;
            }
        }
    }


    template <class InputIterator>
    priority_queue(InputIterator first, InputIterator last)
    {
        c(first, last);
        for (size_t i = (c.size() - 1 - 1) / 2; i >= 0; i++) {
            Adjust_Up(i);
        }
    }

    bool empty() const
    {
        return c.empty();
    }

    size_t size() const
    {
        return c.size();
    }

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

    void push(const T& x)
    {
        c.push_back(x);
        Adjust_Up(c.size() - 1);
    }

    void pop()
    {
        swap(c[0], c[c.size() - 1]);
        c.pop_back();
        Adjust_Down(0);
    }
private:
    Container c;
    Compare comp;
};

这里的仿写利用了vector作为适配器,快速的仿写了一个优先级队列。代码中的优先级队列是一个小根堆,那如果我们要写成一个大根堆,该如何做呢?

答案很简单,就是打开源代码,然后把里面的 < 改成 > 即可。

但是很多时候是不支持修改源代码的,那么又该如何呢?

这时就要用到仿函数了。

template<class T>
class Less 
{
public:
    bool operator() (const T& x, const T& y) const
    {
        return x < y;
    }
};

在C++中,仿函数是一种可被调用的对象,它可以像函数一样接受参数并返回结果。仿函数可以通过函数调用运算符(operator())来实现。

通过定义一个类,重载函数调用运算符(operator()),我们可以创建一个仿函数。这个类的对象可以像函数一样被调用,并且可以像函数一样接受参数和返回结果。

仿函数在很多情况下非常有用,尤其是在需要将函数作为参数传递给其他函数或算法时。通过定义一个仿函数,我们可以将其作为参数传递,并在需要的地方进行函数调用。

那么说回优先级队列要修改成大根堆的问题,我们此时只需要这样做即可修改为大根堆。

template<class T>
class Less 
{
public:
    bool operator() (const T& x, const T& y) const
    {
        return x < y;
    }
};

template<class T>
class Greater
{
public:
    bool operator() (const T& x, const T& y) const
    {
        return x > y;
    }
};

template <class T, class Container = vector<T>, class Compare = less<T> >
class priority_queue
{
public:
    priority_queue()
    {}
    
    void Adjust_Down(size_t parent)
    {
        size_t child = parent * 2 + 1;
        while (child < c.size())
        {
            //if (child + 1 < c.size() && c[child+1] > c[child])
            if (child + 1 < c.size() && comp(c[child+1],c[child]))

            {
                child++;               
            }
            //if (c[child] < c[parent])
            if(cmop(c[child],c[parent]))
            {
                swap(c[child], c[parent]);
                parent = child;
                child = parent * 2 + 1;
            }
            else
            {
                break;
            }
        }
    }

    void Adjust_Up(size_t child)
    {
        size_t parent = (child - 1) / 2;
        while (child > 0)
        {
            //if (c[child] < c[parent])
            if(cmop(c[child],c[parent]))
            {
                swap(c[child], c[parent]);
                child = parent;
                parent = (child - 1) / 2;
            }
            else
            {
                break;
            }
        }
    }


    template <class InputIterator>
    priority_queue(InputIterator first, InputIterator last)
    {
        c(first, last);
        for (size_t i = (c.size() - 1 - 1) / 2; i >= 0; i++) {
            Adjust_Up(i);
        }
    }

    bool empty() const
    {
        return c.empty();
    }

    size_t size() const
    {
        return c.size();
    }

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

    void push(const T& x)
    {
        c.push_back(x);
        Adjust_Up(c.size() - 1);
    }

    void pop()
    {
        swap(c[0], c[c.size() - 1]);
        c.pop_back();
        Adjust_Down(0);
    }
private:
    Container c;
    Compare comp;
};

通过仿函数的传入,此时无论我想改成大根堆或者小根堆,都十分的简单。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值