先来看一段仿写优先级队列的代码
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;
};
通过仿函数的传入,此时无论我想改成大根堆或者小根堆,都十分的简单。