algorithm標頭檔案下的常用函式
1.max(),min(),abs()和swap()
使用例項:
#include#includeusing namespace std;
int main()
執行結果:
1 -2
1 2-2 1
2.reverse()
使用例項1:
#include#includeusing namespace std;
int main()
; reverse(a,a+4);//可以對任意區段進行逆序
for(int i;i<6;i++)
return 0;
}
執行結果:
11 9 8 10 2 9
使用例項2:如果對容器中的元素(例如string字串)進行反轉,結果也是一樣
#include#include#includeusing namespace std;
int main()
; dowhile(next_permutation(a,a+4));
return 0;
}
執行結果:
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
4.fill()
fill()可以把陣列或容器中的某一段區間賦為某個相同的值。和memset不同,這裡的賦值可以是陣列型別對應範圍中的任意值。示例如下:
#include#includeusing namespace std;
int main()
; fill(a,a+5,666);
for(int i;i<5;i++)
return 0;
}
執行結果:
666 666 666 666 666
5.lower_bound()和upper_bound()
lower_bound()和upper_bound()都是用在有序陣列或容器中,lower_bound()是返回第乙個大於或等於要查詢的數,upper_bound()則是返回第乙個大於要查詢的數
#include#includeusing namespace std;
int main()
; int *lowerpos=lower_bound(a,a+10,3);
int *upperpos=upper_bound(a,a+10,3);
printf("%d,%d\n",lowerpos-a,upperpos-a);
return 0;
}
執行結果:
3,6
演算法筆記6 9 algorithm標頭檔案下常用函式
目錄 1.max min abs 2.max element 和min element 求陣列集合以及結構體中最大最小值 3.swap x,y 其實不在algorithm裡 4.reverse 5.next permutation 6.fill 7.sort 8.lower bound 和upper...
algorithm標頭檔案下的函式
非修改性序列操作 12個 迴圈對序列中的每個元素執行某操作 for each 查詢在序列中找出某個值的第一次出現的位置 find 在序列中找出符合某謂詞的第乙個元素 find if 在序列中找出一子串行的最後一次出現的位置 find end 在序列中找出第一次出現指定值集中之值的位置 find fi...
algorithm標頭檔案下函式整合
使用algorithm標頭檔案,在標頭檔案下加一行 using namespace std 才能正常使用 分別返回最大值 最小值 絕對值 注意 max,min中x,y可以是浮點型 abs中的x必須為整數,浮點型用math標頭檔案下的fabs 交換x,y的值 reverse it,it2 可以將陣列指...