algorithm
(algorithm)头文件里面包含了很多十分实用的函数,至少于我而言,这是我用的最多的(stl)。由于所有对区间进行操作的函数都遵循左闭右开的原则,所以假设操作区间为[l,r],那么写在函数里的应该是[l,r+1]。
reverse
翻转一个(vector):(reverse(vector.begin(),vector.end()))
翻转一个数组:(reverse(a+1,a+n+1))
random_shuffle
随机打乱,用法与(reverse)相同。
next_permutation
把操作区间看做一个排列,这个函数就会求比该区间的排列的字典序大的排列的最小的那一个,并且直接在元区间上修改。如果没有下一个了会返回(false),否则会返回(true),同理也有(prev)_(permutation)函数。时间复杂度为(O(n))。
sort
快速排序一个数组:(sort(a+1,a+n+1))。时间复杂度为(O(nlogn))。
lower_bound/upper_bound
(lower)_(bound)((a+1,a+n+1,x))表示在(a)数组里找大于等于(x)的数里最小的那个。
(upper)_(bound)((a+1,a+n+1,x))表示在(a)数组里找大于(x)的数里最小的那个。
返回的是位置的迭代器,内部实现为二分查找,所以查找区间应该提前排好序,时间复杂度为(O(nlogn))。
unique
去重函数。返回去重之后的尾迭代器,左闭右开,所以最后一个值是在尾迭代器(-1)的位置上。也需要操作区间提前排好序,时间复杂度为(O(n))。