1、priority_queue模拟实现
(1)priority_queue.h
#pragma once
#include<vector>
namespace pz
{
template<class T,class Container = vector<T>,class Compare = less<T>>
class priority_queue
{
public:
void AdjustUp(int child)//向上调整,大堆
{
Compare com;
int parent = (child - 1) / 2;
while (child>0)
{
//if (_con[child] > _con[parent]);
if (com(_con[parent], _con[child]))
{
swap(_con[parent], _con[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void AdjustDdown(int root)//向下调整
{
int parent = root;
int child = parent * 2 + 1;
Compare com;
while (child<_con.size())
{
//if (child + 1 < _con.size() && _con[child + 1] > _con[child])
if (child + 1 < _con.size() && com(_con[child],_con[child+1]))
{
++child;
}
if (com(_con[parent], _con[child]))
{
swap(_con[child], _con[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void push(const T& x)
{
_con.push_back(x);
AdjustUp(_con.size() - 1);//向上调整算法
}
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
AdjustDown(0);
}
T& top()
{
return _con[0];
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Container _con;
};
void test()
{
priority_queue<int>a;
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
while (!a.empty())
{
cout << a.top() << " ";
a.pop();
}
cout << endl;
}
}
(2)main.cpp
#include<iostream>
#include"priority_queue.h"
using namespace std;
namespace pz
{
template<class T>//仿函数
struct less//小于号 大堆
{
bool operator()(const T & x1, const T& x2)
{
return x1 < x2;
}
};
template<class T>
struct greater//大于号 小堆
{
bool operator()(const T& x1, const T& x2)
{
return x1>x2;
}
};
}
int main()
{
pz::test();
system("pause");
return 0;
}
仿函数:
又叫它函数对象,因为它的对象可以像函数一样使用
2、stack模拟实现
#include<iostream>
#include<deque>
template <class T, class Con = std::deque<T>>
class Stack
{
public:
Stack() {}
void push(const T&x)
{
_con.push_back(x);
}
void pop()
{
_con.pop_back();
}
T& Top()
{
return _con.back();
}
const T& Top()const
{
return _c.back();
}
size_t Size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Con _con;
};
void test()
{
Stack<int,std::deque<int>>a;
a.push(1);
a.push(2);
a.push(3);
a.push(4);
std::cout << a.Size() << std::endl;
std::cout << a.Top() << std::endl;
a.pop();
a.pop();
std::cout << a.Size() << std::endl;
std::cout << a.Top() << std::endl;
}
int main1()
{
test();
system("pause");
return 0;
}
3、queue模拟实现
#include<deque>
#include<iostream>
template <class T, class Con = std::deque<T>>
class queue
{
public:
queue(){}
void push_back(const T& x)
{
_con.push_back(x);
}
void pop_front()
{
_con.pop_front();
}
T& back()
{
return _con.back();
}
T& front()
{
return _con.front();
}
const T& back()const
{
return _con.back();
}
const T& front()const
{
return _con.front();
}
size_t Size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
private:
Con _con;
};
void test1()
{
queue<int>a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
std::cout << a.Size() << std::endl;
std::cout << a.front() << std::endl;
std::cout << a.back() << std::endl;
a.pop_front();
std::cout << a.Size() << std::endl;
std::cout << a.front() << std::endl;
std::cout << a.back() << std::endl;
}
int main()
{
test1();
system("pause");
return 0;
}