c++函数库函数

1. __gcd(x, y)

求两个数的最大公约数,如__gcd(6, 8) 就返回2。在 algorithm 库中。是不是很方便?

2. reverse(a + 1, a + n + 1)

将数组中的元素反转。a 是数组名,n是长度,跟 sort 的用法一样。值得一提的是,对于字符型数组也同样适用。也在 algorithm 库中。

3. unique(a + 1, a + n + 1) 

去重函数。跟sort的用法一样。不过他返回的值是最后一个数的地址,所以要得到新的数组长度应该这么写: _n = unique(a + 1, a + n + 1) - a - 1.

4.lower_bound(a + 1, a + n + 1, x); upper_bound(a + 1, a + n + 1, x)

lower_bound是查找数组中第一个大于等于x的数,返回该地址,同理也是 pos = lower_bound(a + 1, a + n + 1, x) - a

upper_bound是查找第一个大于x的数,用法和lower_bound一样

复杂度是二分的复杂度,O(logn)。(其实就是代替了手写二分)

猜你喜欢

转载自blog.csdn.net/qq_38734403/article/details/83099105