文章目录
1. max()、min()、abs()、swap()、reverse()
max()
:求两个数最大值
min()
:求两个数最小值
abs()
:求一个数的绝对值,还可使用math.h
里面的fabs()
swap()
:用来交换x和y的值
reverse(Iterator x,Iterator y,)
:翻转 [x,y) 的数组、容器的值。
2. 排序 sort()
sort(first , last , compare)
默认排序:升序
模板2:根据comp返回true的状态排序
数组降序排序:
bool greaterCompare(int a, int b)
{
return a > b;
}
sort(a,a+5,cmp);
结构体排序:
//a.high如果小于b.high,则a结构体的优先级更大, 也就是说:high小的结构体排在前面。
bool Highcmp(Student a, Student b)
{
return a.high < b.high;
}
sort(student, student+10, cmp);
1、sort()排序函数的时间复杂度大概在o(nlogn),比冒泡、简单排序等效率高 。
2、和
reverse()
函数一样,可以自由指定排序范围,也是半开半闭区间(左闭右开)。
3. find() , upper_bound()、lower_bound()、binary_search()
注意:二分查找需要所有元素经过排序。
find(x,y,value)
:
查找某数组指定区间 [x,y) 内是否有x,若有,则返回该位置的地址,若没有,则返回该数组第n+1个值的地址。
1、在数组中查找是否有某值:一定要满足代码中这两个条件。
第一个条件是:p-a != 数组的长度。p是查找数值的地址,a是a[0]的地址。
第二个条件是:*p == x; 也就是该地址指向的值等于我们要查找的值。
最后输出p-a+1; p-a相当于x所在位置的地址a[0]所在位置的地址, 但因为是从0开始算, 所以最后需要+1。
upper_bound()
:查找第一个大于x的值的位置
lower_bound()
:查找第一个大于等于x的值的位置
同样是返回地址,用法和find()
函数一样,限制条件也一样,照着用就可以。
binary_search(first,last,value)
range [first,last)中的元素至少有一个等于val,返回true,否则返回false。
序列应该按照默认升序或者comp规则排序。
4.fill()、count()、__gcd()、next_permutation()、
fill(first,last,value)
: 使用val填充 range [first,last) 。
__gcd()
:求最大公因数
用二者乘积除以最大公因数即可得到最小公倍数。 因此没有求最小公倍数的函数。
count(first,last,value)
在range [first,last)中查找value出现的次数.
next_permutation(first,last)
将给定区间的数组、容器全排列.
next_permutation()
函数接受两个迭代器参数:first和last,表示要生成下一个排列的范围。这个范围包含了一系列元素,可以是数组、容器或其他可遍历的结构。
返回值:
如果存在下一个排列,则函数返回true,并将范围内的元素重新排列为下一个排列;如果不存在下一个排列(已经是最后一个排列),则函数返回false,并将范围内的元素重新排列为第一个排列。
注意事项:
- 要使用next_permutation()函数,必须先确保范围内的元素已按照非降序进行排序。
- 如果范围内的元素已经是最后一个排列,调用next_permutation()函数将会将范围内的元素重新排列为第一个排列,并返回false。
举例:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {1, 2, 3};
// 打印初始排列
cout << "Initial permutation: ";
for (int num : nums) {
cout << num << " ";
}
// 生成下一个排列
while (std::next_permutation(nums.begin(), nums.end()))
{
cout << "\nNext permutation: ";
for (int num : nums)
{
cout << num << " ";
}
}
return 0;
}
//输出结果
Initial permutation: 1 2 3
Next permutation: 1 3 2
Next permutation: 2 1 3
Next permutation: 2 3 1
Next permutation: 3 1 2
Next permutation: 3 2 1
5.unique()、transform()
unique(first,last)
在range[first,last)中,如果遇到连续的相同元素,只保留第一个。并返回处理完毕之后的end迭代器。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> nums = { 1, 1, 2, 2, 3, 4, 4, 5, 5 };
vector<int>::iterator it = std::unique(nums.begin(), nums.end());
for (vector<int>::iterator iter = nums.begin(); iter != it; ++iter) {
cout << *iter << " ";
}
return 0;
}
输出:
1 2 3 4 5
transform()
:对给定范围内的元素应用指定的操作,并将结果存储在另一个范围中。
语法:
template <class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator transform (InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperation op);
transform()函数接受四个参数:
first1和last1:定义了要进行转换的输入范围,表示一系列元素的起始和结束位置。
result:定义了存储转换结果的输出范围,即另一个容器或数组。
op:是一个可调用对象(函数、函数指针、函数对象或lambda表达式),用于对输入范围内的每个元素进行转换操作。
返回值:
transform()
函数返回一个迭代器,指向转换结果范围的末尾。
注意事项:
输入范围和输出范围可以相同,但不应该有重叠的部分。
输入范围和输出范围的大小应该相等。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int square(int x)
{
return x * x;
}
int main()
{
vector<int> nums = { 1, 2, 3, 4, 5 };
vector<int> squared_nums(nums.size());
transform(nums.begin(), nums.end(), squared_nums.begin(), square);
for (int num : squared_nums)
{
cout << num << " ";
}
return 0;
}
// 平方后的结果:{1, 4, 9, 16, 25}
输出结果
1 4 9 16 25
尾声
本文主要讲了一些比较常见的算法函数,如果有错误的地方,请大家及时指出,谢谢。