STL常用算法

tiny_star Lv3

一些好用的算法函数,能记就记吧,记不住还得自己写😭😭😭

头文件<algorithm>

1.全排列函数next_permutation

bool next_permutation(start,end)是C++中常用的全排列函数。
它的作用是求当前排列的下一个排列(按字典序升序的下一个排列)。当前排列的下一个排列不存在时,返回false,否则返回true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 输出数组a的全排列
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[4]={1,2,3,4};
cout<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
while(next_permutation(a,a+4))
{
cout<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
}
return 0;
}

2.序列排序函数sort

3.二分查找函数

lower_bound():返回大于或等于目标值的第一个位置
upper_bound():返回大于目标值的第一个位置
binary_search():若目标值存在则返回true,否则返回false

4.序列容器遍历函数

find():
find_if():
equal():
count():
for_each():

5.序列修改函数

swap():
replace():
replace_if():

头文件<cstring>

头文件<cmath>

头文件<numeric>

accumulate

头文件<cstdlib>

atoi():

  • Titre: STL常用算法
  • Auteur: tiny_star
  • Créé à : 2025-08-01 21:55:48
  • Mis à jour à : 2025-08-03 13:09:56
  • Lien: https://tiny-star3.github.io/2025/08/01/Algorithm/STL常用算法/
  • Licence: Cette œuvre est sous licence CC BY-NC-SA 4.0.
Commentaires