STL与泛型

STL是指标准模板库(Standard Template Library)。

其中,用处很大的有容器泛型算法

这些在C++ Primer中有详细讲解,所以直接贴C++ Primer笔记了。

注意,部分竞赛不支持C++11

顺序容器

https://www.yuque.com/gaoshanliushui-mbfny/cc3os4/qnd261

关联容器

https://www.yuque.com/gaoshanliushui-mbfny/cc3os4/kmno0n

泛型算法

https://www.yuque.com/gaoshanliushui-mbfny/cc3os4/sq7mak

补充

priority_queue

  • 优先队列,我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队。

  • 头文件queue

  • 定义:priority_queue<Type, Container, Functional>
    Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector),Functional 就是比较的方式。
    当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,默认是大顶堆。 ```cpp //升序队列,小顶堆 priority_queue ,greater > q; //降序队列,大顶堆 priority_queue ,less >q;

//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了) ```

  • 和队列基本操作相同:

    • top 访问队头元素
    • empty 队列是否为空
    • size 返回队列内元素个数
    • push 插入元素到队尾 (并排序)
    • emplace 原地构造一个元素并插入队列
    • pop 弹出队头元素
    • swap 交换内容

lower_bound

https://www.cplusplus.com/reference/algorithm/lower_bound/?kw=lower_bound

upper_bound

https://www.cplusplus.com/reference/algorithm/upper_bound/?kw=upper_bound

set_union

https://www.cplusplus.com/reference/algorithm/set_union/?kw=set_union

set_intersection

https://www.cplusplus.com/reference/algorithm/set_intersection/?kw=set_intersection

例题

UVA12096 集合栈计算机 The SetStack Computer

https://www.yuque.com/gaoshanliushui-mbfny/eif9pg/gig547