STL max_element
来源:互联网 发布:单片机实验教程 编辑:程序博客网 时间:2024/06/05 20:29
取容器中的最大或最小值:max_element, min_element。
代码
//see: http://www.cplusplus.com/reference/algorithm/max_element/#include <iostream>#include <list>#include <algorithm>bool my_compare(int x, int y){ return abs(x) < abs(y);}class MyCompare {public: bool operator()(int x, int y) { return abs(x) < abs(y); }};int main(){ std::list<int> values; values.push_back(1); values.push_back(3); values.push_back(-3); values.push_back(-1); int the_max = *std::max_element(values.begin(), values.end()); int the_min = *std::min_element(values.begin(), values.end()); std::cout<<"max: "<<the_max<<std::endl; std::cout<<"min: "<<the_min<<std::endl; the_max = *std::max_element(values.begin(), values.end(), my_compare); the_min = *std::min_element(values.begin(), values.end(), my_compare); std::cout<<"max: "<<the_max<<std::endl; std::cout<<"min: "<<the_min<<std::endl; MyCompare myCompare; the_max = *std::max_element(values.begin(), values.end(), myCompare); the_min = *std::min_element(values.begin(), values.end(), myCompare); std::cout<<"max: "<<the_max<<std::endl; std::cout<<"min: "<<the_min<<std::endl; return 0;}
运行结果
max: 3min: -3max: 3min: 1max: 3min: 1
0 0
- STL max_element
- stl min_element和max_element
- STL max_element,min_element
- C++ STL之max_element()/min_element()
- max_element
- max_element
- max_element
- max_element
- *max_element
- STL 之count,count_if,max,max_element,min,min_element和random_shuffle
- STL 之count,count_if,max,max_element,min,min_element和random_shuffle
- STL常用算法: max_element,min_element, distance以及手动实现.
- C++ *max_element函数找最大元素 *min_element函数找最小元素 STL算法
- C++ *max_element函数找最大元素 *min_element函数找最小元素 STL算法
- C++ 中*max_element函数找最大元素 *min_element函数找最小元素 STL算法
- STL fill()填充算法以及for_each()算法 取容器中的最大最小值min_element,max_element
- STL之min_element()与max_element()(取容器中的最大最小值)
- C++ *max_element函数找最大元素 *min_element函数找最小元素 STL算法
- spring applicationContext.xml 配置文件 详解
- Android仿支付宝首页GridView的网格效果-不能够换位置
- Android Studio设置记录
- 51单片机系列知识7--中断系统(2)
- java中的IO整理(上)(微信文章)
- STL max_element
- 无人职守安装sles11
- 【源码分享】WPF漂亮界面框架实现原理分析及源码分享
- Android中自定义控件的步骤
- 开始我的博客时代
- centos6.5下忘记mysql密码
- 自学 cocos2d 游戏开发应该按什么步骤进行?
- php学习路线图
- #leetcode# 9 Palindrome Number