1、算法
![](https://img-blog.csdnimg.cn/direct/b25ae4dc0dfe4f66881433ed8a039e15.png)
2、算法分类
![](https://img-blog.csdnimg.cn/direct/f3a052ea0a8f4de7bab50685563e4bc3.png)
3、非变动性算法
![](https://img-blog.csdnimg.cn/direct/3eb3874a60cd47149a1d7e12cfe845a6.png)
4、变动性算法
![](https://img-blog.csdnimg.cn/direct/90d458fd56524e8299e030715dcc16b0.png)
5、移除性算法
![](https://img-blog.csdnimg.cn/direct/9af8ed93377345d083a03d66885f482f.png)
6、变序性算法
![](https://img-blog.csdnimg.cn/direct/07439c38f0be4f538111f433fd58ffd7.png)
7、排序算法
![](https://img-blog.csdnimg.cn/direct/c020499ef5c94647aee9723feb1a11bc.png)
8、已序区间算法
![](https://img-blog.csdnimg.cn/direct/5db99b588944488591bce448367d2965.png)
9、数值算法
![](https://img-blog.csdnimg.cn/direct/4eae245b43154fcea246ff8acb65a289.png)
10、算法尾词
![](https://img-blog.csdnimg.cn/direct/eaec433dd70d4b9f8c70004a09d19de4.png)
11、非变动性算法示例
![](https://img-blog.csdnimg.cn/direct/d71db3fb053f45649a65162dba67e71d.png)
在使用的时候,会拷贝一份出来,所以不会对原数据有影响。主要是要点进去看内部源码的实现。
cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print_element(int n)
{
cout << n << ' ';
}
bool bigger_than_4(int n)
{
return n > 4;
}
int main() {
int a[] = {1, 2, 3, 4, 5};
vector<int> v(a, a+5);
vector<int>::const_iterator it;
for(it = v.begin(); it != v.end(); ++it)
{
cout << *it << ' ';
}
cout << endl;
// 这个其实就是用for循环遍历,然后把每次遍历的元素当作参数传给print_element去执行
// 所以print_element中参数的类型要和v的类型一致
for_each(v.begin(), v.end(), print_element);
cout << endl;
it = min_element(v.begin(), v.end());
if (it != v.end())
{
cout << *it << endl;
}
it = max_element(v.begin(), v.end());
if (it != v.end())
{
cout << *it << endl;
}
it = find(v.begin(), v.end(), 4);
if (it != v.end())
{
cout << it - v.begin() << endl;
}
else
{
cout << "not found" << endl;
}
it = find_if(v.begin(), v.end(), bigger_than_4);
if (it != v.end())
{
cout << it - v.begin() << endl;
}
else
{
cout << "not found" << endl;
}
return 0;
}
// 输出
1 2 3 4 5
1 2 3 4 5
1
5
3
4