algorithm头文件函数全集——史上最全,最贴心
2022.4.8更:
隨著本篇博客觀看次數越來越多,假如有一點點疏忽,就可能造成更大的影響, 因此采取動態維護的策略:
從今天開始,每天我會檢查評論區, 及時解答大家的疑問,修改可能存在的問題 如果哪里寫的有疏漏,也歡迎批評指出!
簡介:
algorithm頭文件是C++的標準算法庫,它主要應用在容器上。 因為所有的算法都是通過迭代器進行操作的,所以算法的運算實際上是和具體的數據結構相分離的 ,也就是說,具有低耦合性。 因此,任何數據結構都能使用這套算法庫,只要它具有相應的迭代器類型。
常用函數:
一、max()、min()、abs()函數
max():求兩個數最大值
min():求兩個數最小值
abs():求一個數的絕對值
代碼:
#include<iostream> #include<algorithm> using namespace std; int main() {int a = 3, b = 4;//求最大值int Max = max(a,b);//求最小值int Min = min(a,b);//求絕對值int Abs = abs(-3);cout << Max << Min << Abs;return 0;}輸出:433
注意:
1、max()和min()函數中的參數只能是兩個,如果想求3個數的最大值,需要嵌套一下
同理:如果想求數組中的最大值,需要在循環中寫。
2、寫了algorithm頭文件后, max就變成了函數名,在自己定義變量時,要避免使用max,min等。
3、abs()函數只能用于求整型變量的絕對值,而#include<cmath>中的fabs()函數還可用于求浮點型變量的絕對值,不要搞混~
2、交換函數:swap()
用來交換x和y的值
代碼:
#include<iostream> #include<algorithm> using namespace std; int main() {int a = 3, b = 4;swap(a,b);cout << a << b;return 0;}輸出:43
3、翻轉函數:reverse()
翻轉x-y區間的數組、容器的值。
1、翻轉整個數組
翻轉整個數組:
#include<iostream> #include<algorithm> using namespace std; int main() {int a[5] = {11,22,33,44,55};reverse(a,a+5);for(int i = 0; i < 5; i++) cout << a[i] << ' ';return 0;}輸出:55 44 33 22 11
2、也可以實現對部分值的翻轉,像這樣:
翻轉部分數組:
#include<iostream> #include<algorithm> using namespace std; int main() {int a[5] = {11,22,33,44,55};reverse(a+3,a+5);for(int i = 0; i < 5; i++) cout << a[i] << ' ';return 0;}輸出:11 22 33 55 44
3、翻轉容器:若想對容器中所有的數進行翻轉,則需要用到begin()、end()函數,像這樣:
翻轉整個容器:
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() {vector<int>v;//輸入: for(int i = 0; i < 5; i++) v.push_back(i);//輸出: reverse(v.begin(), v.end());for(int i = 0; i < v.size(); i++) {cout << v[i] << ' ';}return 0;}輸出:4 3 2 1 0
4、翻轉容器:容器的翻轉也可以用迭代器,來實現指定位數的翻轉,像這樣:
翻轉部分容器:
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() {vector<int>v;vector<int>::iterator it;//輸入: for(int i = 0; i < 5; i++) v.push_back(i);//輸出: it = v.begin(); reverse(it, it+3);for(int i = 0; i < v.size(); i++) {cout << v[i] << ' ';}return 0;}輸出:2 1 0 3 4
注意:
如果想在翻轉時指定位數,則其為半開半閉區間。 如reserve(a+2, a+4);翻轉數組中第2-4之間的數,不包括第二個,但包括第四個。
四、排序函數:sort()
1、對x-y區間的數組、容器進行排序。默認升序排列。
數組升序排序:
#include<iostream> #include<algorithm> using namespace std; int main() {int a[5] = {55,44,33,22,11};sort(a,a+5);for(int i = 0; i < 5; i++) cout << a[i] << ' ';return 0;}輸出:11 22 33 44 55
2、如果想將數組降序排序,就需要寫一個簡單的函數,改變默認的排序功能,像這樣:
數組降序排序:
#include<iostream> #include<algorithm> //意思是:若a>b,則a的優先級更大! 也就是說大的在前面。 bool cmp(int a, int b) {return a > b; } using namespace std; int main() {int a[5] = {55,44,33,22,11};sort(a,a+5,cmp); //這里需要加上自己自定義的函數for(int i = 0; i < 5; i++) cout << a[i] << ' ';return 0;}輸出:55 44 33 22 11
3、同理,如果想對結構體排序,也需要自定義優先級。像這樣:
結構體排序:
#include<iostream> #include<algorithm> using namespace std; //用sort函數對結構體排序 struct Student {int high;int weigh; }student[10]; //a.high如果小于b.high,則a結構體的優先級更大, 也就是說:high小的結構體排在前面。 bool cmp(Student a, Student b) {return a.high < b.high; } int main() {for(int i = 0; i < 10; i++) {student[i].high = i ;}sort(student, student+10, cmp); //將自定義的函數添加上。for(int i = 0; i < 10; i++) {cout << student[i].high << ' ';}return 0; }輸出:0 1 2 3 4 5 6 7 8 9
4、如果想對容器排序,就需要使用迭代器,或begin(),end()函數。像這樣:
容器升序排序:
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() {vector<int>v;vector<int>::iterator it;//輸入: for(int i = 5; i > 0; i--) v.push_back(i); //輸出: it = v.begin(); sort(it, it+3); // sort(v.begin(), v.end())for(int i = 0; i < v.size(); i++) {cout << v[i] << ' ';}return 0;}輸出:3 4 5 2 1
5、同理,如果想對容器降序排序,或對容器結構體排序,向數組那樣操作就可以了。
注意:
1、sort()排序函數的時間復雜度大概在o(nlogn),比冒泡、簡單排序等效率高 。
2、和reverse()函數一樣,可以自由指定排序范圍,也是半開半閉區間(左閉右開)。
五、查找函數:find()
查找某數組指定區間x-y內是否有x,若有,則返回該位置的地址,若沒有,則返回該數組第n+1個值的地址。(好煩有木有,為啥要返回地址。還要轉化o(╥﹏╥)o)
1、數組中查找是否有某值:一定一定一定要滿足代碼中這兩個條件。
第一個條件是:p-a != 數組的長度。p是查找數值的地址,a是a[0]的地址。
第二個條件是:*p == x; 也就是該地址指向的值等于我們要查找的值。
最后輸出p-a+1; p-a相當于x所在位置的地址-a[0]所在位置的地址, 但因為是從0開始算, 所以最后需要+1。
對數組查找:
#include<iostream> #include<algorithm> using namespace std; int main() { int a[5] = {11,22,33,44,55} int *p = find(a,a+5,33); //定義指針,指向查找完成后返回的地址,5為a2數組長度 if(((p-a) != 5) && (*p == x)) //若同時滿足這兩個條件,則查找成功,輸出 cout << (p-a+1); //輸出所在位置 return 0;}輸出:3
2、對容器進行查找同理:也要滿足這兩個條件:
對容器查找:
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() {vector<int>v;vector<int>::iterator it, it1;//輸入: for(int i = 0; i < 5; i++) v.push_back(i); //查找 int size = v.size(); //第一步:求長度it = find(v.begin(), v.end(), 3); //第二步:查找x在容器的位置,返回迭代器1it1 = v.begin(); //第三步:令迭代器2指向容器頭 if(((it-it1)!=size)&&(*it==3)) //第四步:若同時滿足這兩個條件,則查找成功,輸出 cout << (it-it1+1) << endl; //輸出所在位置 return 0;}輸出:4
六、查找函數:upper_bound()、lower_bound()
1、upper_bound():查找第一個大于x的值的位置
2、lower_bound():查找第一個大于等于x的值的位置
同樣是返回地址,用法和find()函數一毛一樣,限制條件也一毛一樣,照著扒就行了。
七、填充函數:fill()
在區間內填充某一個值。同樣適用所有類型數組,容器。
1、舉例:在數組中未賦值的地方填充9999
代碼:
#include<iostream> #include<algorithm> using namespace std; int main() { int a[5] = {11,33,22};fill(a+3,a+5,9999); for(int i = 0; i < 5; i++) cout << a[i] << ' ';return 0;}輸出:11 33 22 9999 9999
應用:
常用在大數加法中,因為數太大,需要用字符串保存,如果在運算時需要填充0,就要用這個函數。
八、查找某值出現的次數:count()
1、在數組中查找x 在某區間出現的次數:
代碼:
#include<iostream> #include<algorithm> using namespace std; int main() { int a[5] = {11,22,33,44,44};cout << count(a, a+5, 44); return 0;}輸出:2
2、在容器中查找同理,只是需要用iterator迭代器或begin()、end()函數。
注意:
和前幾個函數一樣,如果需要指定區間查詢,注意是半開半閉區間(左閉右開區間)。
八、求最大公因數:__gcd()
震驚把!在我最開始知道竟然有這個函數時,我也是震驚的!
另外,用二者乘積除以最大公因數即可得到最小公倍數。 因此沒有求最小公倍數的函數。
代碼:
#include<iostream> #include<algorithm> using namespace std; int main() { int a = 12, b = 4;int Gcd = __gcd(a,b);cout << Gcd;return 0;}注意:
__gcd() 需要寫兩個下劃線!
九、求交集、并集、差集:set_intersection()、set_union()、set_difference()
1、求交集:
(1):將兩個數組的交集賦給一個容器(為什么不能賦給數組呢?因為數組不能動態開辟,且inserter()函數中的參數必須是指向容器的迭代器。):
代碼:
#include<algorithm> #include<iostream> #include<vector> using namespace std; int main() {int a[5] = {1,2,3,4,5}, b[5] = {1,2,33,44,55};vector<int>::iterator it;vector<int>v4; set_intersection(a, a+5, b, b+5, inserter(v4,v4.begin()));for(int i = 0; i < v4.size(); i++) {cout << v4[i] << ' ';}輸出:1 2
(2):將兩個容器的交集賦給另一個容器:
代碼:
#include<algorithm> #include<iostream> #include<vector> using namespace std; int main() {vector<int> v1, v2, v3;for(int i = 0; i < 5; i++) v1.push_back(i);for(int i = 3; i < 8; i++) v2.push_back(i);set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3,v3.begin())); for(int i = 0; i < v3.size(); i++) {cout << v3[i] << ' ';}return 0; }輸出:3 4
2、求并集:
(1):將兩個數組的并集賦給一個容器(為什么不能賦給數組呢?因為數組不能動態開辟,且inserter()函數中的參數必須是指向容器的迭代器。):
代碼:
#include<algorithm> #include<iostream> #include<vector> using namespace std; int main() {int a[5] = {1,2,3,4,5}, b[5] = {1,2,33,44,55};vector<int>::iterator it;vector<int>v4; set_union(a, a+5, b, b+5, inserter(v4,v4.begin()));for(int i = 0; i < v4.size(); i++) {cout << v4[i] << ' ';}輸出:1 2 3 4 5 33 44 55
(2):將兩個容器的并集賦給另一個容器:
代碼:
#include<algorithm> #include<iostream> #include<vector> using namespace std; int main() {vector<int> v1, v2, v3;for(int i = 0; i < 5; i++) v1.push_back(i);for(int i = 3; i < 8; i++) v2.push_back(i);set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3,v3.begin())); for(int i = 0; i < v3.size(); i++) {cout << v3[i] << ' ';}return 0; }輸出:0 1 2 3 4 5 6 7
3、差集完全同理。
注意:
inserter(c,c.begin())為插入迭代器
此函數接受第二個參數,這個參數必須是一個指向給定容器的迭代器。元素將被插入到給定迭代器所表示的元素之前。
十、全排列:next_permutation()
將給定區間的數組、容器全排列
1、將給定區間的數組全排列:
代碼:
#include<iostream> #include<algorithm> using namespace std; int main() { int a[3] = {1,2,3};do{cout<<a[0]<<a[1]<<a[2]<<endl; }while(next_permutation(a,a+3)); //輸出1、2、3的全排列 return 0;}輸出:
123
132
213
231
312
321
2、容器全排列同理:只不過將參數換成iterator迭代器或begin()、end()函數。
注意:
和之前的一樣,如果指定全排列區間,則該區間是半開半閉區間(左閉右開)
/*_ooOoo_o8888888o88" . "88(| -_- |)O\ = /O____/`---'\____.' \\| |// `./ \\||| : |||// \/ _||||| -:- |||||- \| | \\\ - /// | || \_| ''\---/'' | |\ .-\__ `-` ___/-. /___`. .' /--.--\ `. . __."" '< `.___\_<|>_/___.' >'"".| | : `- \`.;`\ _ /`;.`/ - ` : | |\ \ `-. \_ __\ /__ _/ .-` / / ======`-.____`-.___\_____/___.-`____.-'======`=---=' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^I have a dream! An AC deram!!*/如果哪里有困惑,歡迎給筆者留言。
如果這篇博文對你產生了幫助,可以留下小小的一個贊哦,大家的支持是我更新的最大動力~
總結
以上是生活随笔為你收集整理的algorithm头文件函数全集——史上最全,最贴心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【已解决】Errors during d
- 下一篇: 【最新合集】PAT乙级最优题解(题解+解