算法无处不在,但是有些算法是通用的.比如排序,查找等.为了提高c++代码的利用率,STL专门写了algorithms这个模板.

不修改内容的序列操作

通常是各种findsearch操作

修改内容的序列操作

通常是各种copy,move,remove,replace,reverse,swap等操作

划分操作

按照某个函数,对容器的数据进行重新划分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include <array>
#include <algorithm>
bool IsOdd (int i) { return (i%2)==1; }
int main(){
std::array<int,7> foo {1,2,3,4,5,6,7};
// print contents:
std::cout << "foo:";
for (int& x:foo)
std::cout << ' ' << x;
if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )
std::cout << " (partitioned)\n";
else
std::cout << " (not partitioned)\n";
// partition array:
std::stable_partition(foo.begin(),foo.end(),IsOdd);
// print contents again:
std::cout << "foo:";
for (int& x:foo)
std::cout << ' ' << x;
if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )
std::cout << " (partitioned)\n";
else
std::cout << " (not partitioned)\n";
return 0;
}

排序操作

各种sort操作

二分法查找操作

各种search操作

集合操作

交集,并集,补集等

堆操作

其实就是一个堆排序.

最大/最小操作

一堆max,min函数.