C++头文件algorithm 3——Min/Max
来源:互联网 发布:华沙 知乎 编辑:程序博客网 时间:2024/06/05 23:03
min
example
#include<iostream>#include<algorithm>using namespace std;int main(){ cout << "min(4,5)=" << min(4, 5) << endl; cout << "min('a','n')=" << min('a', 'n') << endl; cout << "min(5.6,3.12)=" << min(5.6, 3.12) << endl; return 0;}
output
max
example
#include<iostream>#include<algorithm>using namespace std;int main(){ cout << "max(4,5)=" << max(4, 5) << endl; cout << "max('a','n')=" << max('a', 'n') << endl; cout << "max(5.6,3.12)=" << max(5.6, 3.12) << endl; return 0;}
output
minmax
结果返回一对值:a和b。第一元素是最小值,第二个元素是最大值。
example
#include<iostream>#include<algorithm>using namespace std;int main(){ auto res = minmax({ 1, 2, 3, 4, 5, 6, 7, 8, 9 }); cout << "minmax({ 1, 2, 3, 4, 5, 6, 7, 8, 9 })\t"; cout << "min:" << res.first << "\tmax:" << res.second << endl; auto res2 = minmax({ 1,1,1,1,1,1,1,1,1}); cout << "minmax({ 1,1,1,1,1,1,1,1,1 })\t"; cout << "min:" << res2.first << "\tmax:" << res2.second << endl; return 0;}
output
min_element&max_element
ForwardIterator min_element (ForwardIterator first, ForwardIterator last, Compare comp);
结果返回一个迭代器,它指向某个范围内最小或最大元素;如果这个范围为空,则返回comp。
example
#include<iostream>#include<algorithm>using namespace std;bool myfunction(int i, int j){ return (i < j); }struct myclass{ bool operator()(int i, int j){ return i < j; }}myobject;int main(){ int myints[] = {4,7,8,2,9,5}; //using default comparison cout << "The smallest element is" << *min_element(myints, myints + 6)<<endl; cout << "The largest element is " << *max_element(myints, myints + 6)<<endl; //using function as comp cout << "The smallest element is" << *min_element(myints, myints + 6,myfunction)<<endl; cout << "The largest element is " << *max_element(myints, myints + 6,myfunction)<<endl; //using object as comp cout << "The smallest element is" << *min_element(myints, myints + 6,myobject)<<endl; cout << "The largest element is " << *max_element(myints, myints + 6,myobject)<<endl; return 0;}
output
minmax_element
example
#include<iostream>#include<algorithm>#include<vector>using namespace std;int main(){ vector<int> myvector= { 4, 7, 8, 2, 9, 5 }; auto res = minmax_element(myvector.begin(), myvector.end()); cout << "all element :"; for (auto x : myvector) cout << x << " "; cout << "\nmin:\t" << *res.first <<" at position: "<<(res.first-myvector.begin())<< endl; cout << "max\t" << *res.second << " at position: " << (res.second-myvector.begin())<< endl; return 0;}
output
阅读全文
0 0
- C++头文件algorithm 3——Min/Max
- C语言:min和max头文件
- 找不到min和max标识符—C++中头文件次序问题
- min,max和abs函数不同编译器不同头文件
- Min-Max Max-Min problem algorithm and analysis
- C/C++笔试必须熟悉掌握的头文件系列(八)——algorithm
- c++-algorithm 头文件排序sort
- c++-algorithm 头文件排序sort
- C++ 头文件algorithm 1——partition
- C++头文件algorithm 2——Sorting
- Algorithm头文件简介
- algorithm头文件源码
- algorithm头文件
- Algorithm头文件学习
- C++ <algorithm>头文件
- <algorithm>头文件
- 找不到min和max标识符--C++中头文件次序问题
- 找不到min和max标识符--C++中头文件次序问题
- 收藏的文章,随时更新
- 嵌入式Linux学习笔记(2)——vi编辑器
- python3.6 mysql django mysql-python
- HDU 1081 求最大子矩形 dp
- POJ 3709 Round Numbers 数位dp
- C++头文件algorithm 3——Min/Max
- linux学习---内存管理以及结存结构描述
- HDU 4532 组合数+动态规划
- 权限
- Kali下压缩解压缩命令大全zip,tar,tar.gz,tar.bz2(转)
- Construct 2初体验
- 脏读1
- 请求方式的集合
- 2017年10月08日普及组 世界语