STL学习记录(十四):其它几种算法
来源:互联网 发布:怎样用淘宝客推广 编辑:程序博客网 时间:2024/06/02 16:42
其它几种算法Paritition、Heap、Min/Max
在介绍前面几种算法后,剩下的几种算法将在这一部分集中介绍。Partition算法主要是根据定义的规则,将范围内的元素分为两部分。
Heap算法主要是关于堆部分的算法。而Min/Max类算法主要是关于数值部分的算法。算法说明与示例如下:
代码示例:
// partition algorithm example#include <iostream> #include <algorithm> #include <vector>using namespace std;bool IsOdd(int i) { return (i % 2) == 1; }int main() { vector<int> myvector{ 1, 2 ,3 ,4, 5 ,6 ,7 ,8 ,9 }; vector<int>::iterator bound; bound = partition(myvector.begin(), myvector.end(), IsOdd); // print out content: cout << "odd elements: "; for (vector<int>::iterator it = myvector.begin(); it != bound; ++it) cout << *it << ' '; cout << endl; cout << "even elements: "; for (vector<int>::iterator it = bound; it != myvector.end(); ++it) cout << *it << ' '; cout << endl; return 0;}//output://odd elements: 1 9 3 7 5//even elements: 6 4 8 2
// range heap example#include <iostream> #include <algorithm> #include <vector>using namespace std;int main () { vector<int> v{10,20,30,5,15};; make_heap (v.begin(),v.end()); cout << "initial max heap : " << v.front() << '\n'; pop_heap (v.begin(),v.end()); v.pop_back(); cout << "max heap after pop : " << v.front() << '\n'; v.push_back(99); push_heap (v.begin(),v.end()); cout << "max heap after push: " << v.front() << '\n'; sort_heap (v.begin(),v.end()); cout << "final sorted range :"; for (unsigned i=0; i<v.size(); i++) cout << ' ' << v[i]; cout << '\n'; return 0;}//output://initial max heap : 30//max heap after pop : 20//max heap after push: 99//final sorted range : 5 10 15 20 99
// minmax example#include <iostream> #include <algorithm>using namespace std; int main () { auto result = minmax({1,2,3,4,5}); cout << "minmax({1,2,3,4,5}): "; cout << result.first << ' ' << result.second << '\n'; return 0;}//output://minmax({1,2,3,4,5}): 1 5
0 0
- STL学习记录(十四):其它几种算法
- STL(二十四)数值算法
- STL学习记录(三):算法基础简要
- STL学习记录(十一):修改类算法
- STL学习记录(十二):非修改类算法
- mysql 学习记录(十四)--模式
- 无人机驾驶员培训学习记录(十四)
- 算法学习记录十四(C++)--->二叉树的镜像
- STL学习记录(一):STL简介
- STL标准算法(三)其它算法
- 几种STL排序算法比较
- STL学习记录(十三):排序类算法及基于排序的算法
- 算法学习(十四)最大公约数问题
- oracle学习记录之十四
- STL学习之十四:常用算法 for_each 和 transform以及adjacent_find的示例代码
- android学习记录(十四)android震动功能调用
- mysql 学习记录(二十四)--mysql相关工具
- (转)几种流形学习算法
- POJ3258—River Hopscotch—二分法求the largest mimmun
- Qt实战之开发CSDN下载助手 (3)(结束篇)
- SSH开发——菜鸟那些事儿
- zoj 3882 Help Bob(博弈)
- 循环、数组截取、随机数的产生(2015/7/30)
- STL学习记录(十四):其它几种算法
- VS2010关于error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- ARM处理器体系架构详细说明
- uvalive 4973
- Scala学习第五天:数组的基本操作,数组的进阶操作和多维数组
- Hdu 1789 Doing Homework again
- pycharm快捷键、常用设置、包管理
- Selenium IDE简介
- linux驱动中宏__setup(str, fn)