第二章 STL
仿函数
仿函数是一个类,它的对象叫做函数对象。函数对象可以像函数一样去使用。仿函数的类要求重载一个函数:operator()
namespace hek4
{
// 除了默认限定符不一样,struct(默认public)和class在C++中是一样的
// 一般情况,成员部分私有,部分公有就用class
// 所有成员都公有,就用struct。
// 仿函数 函数对象
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()
{
hek4::less<int> lessfunc;
cout << lessfunc(1, 2); // lessfunc.operator()
test_sort();
return 0;
}
基本使用
vector<int> v;
v.push_back(5);
v.push_back(4);
v.push_back(9);
v.push_back(3);
v.push_back(8);
// 升序 less <
sort(v.begin(), v.end());
for (auto& i : v)
{
cout << i << " ";
}
cout << endl;
// 降序 greater >
/*greater<int> gt;
sort(v.begin(), v.end(),gt);*/
sort(v.begin(), v.end(), hek4::greater<int>() ); // 匿名对象
for (auto& i : v)
{
cout << i << " ";
}
cout << endl;
优先级队列 priority_queue
1.优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。
2.此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素)
3.优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。
4.底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:
empty():检测容器是否为空
size():返回容器中有效元素个数
front():返回容器中第一个元素的引用
push_back():在容器尾部插入元素
pop_back():删除容器尾部元素
5.标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。
6.需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。
使用
void test_priority_queue1()
{
//priority_queue<int> pq; // 默认大的优先级高 // 升序
priority_queue<int ,vector<int>,greater<int> > pq; // 降序
pq.push(2);
pq.push(1);
pq.push(9);
pq.push(5);
pq.push(7);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
模拟实现
namespace hek3
{
// 仿函数
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;
}
};
// 默认是大堆
template<class T,class Container = vector<T>,class Comapre = less<T> >
class priority_queue
{
public:
void AdjustUp(int child)
{
Comapre com;
int parent = (child - 1) / 2;
while (child > 0)
{
//if (_con[child] > _con[parent]) // 降序
// 降序是使用小于号实现的 less 仿函数 与官方一致
if (com(_con[parent],_con[child])) // 升序和降序都可以实现
{
swap(_con[child], _con[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void push(const T& x)
{
_con.push_back(x);
// O(logN)
AdjustUp(_con.size() - 1); // 向上调整
}
void AdjustDWon(int root)
{
Comapre com;
int parent = root;
int child = parent * 2 + 1; // 左孩子
while (child < _con.size())
{
// 选出左右孩子中大的那一个
//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 (com(_con[parent], _con[child]))
{
swap(_con[child], _con[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
// O(logN)
AdjustDWon(0); // 向下调整
}
T& top(){return _con[0];}
size_t size(){return _con.size();}
bool empty(){return _con.empty();}
private:
Container _con;
};
}