c++之STL(12) STL 算法 - 最小值和最大值
程序员文章站
2023-04-02 13:46:40
min_element(b , e)
min_element(b , e, op)
max_element(b , e)
max_element(b , e, op)
//...
min_element(b , e)
min_element(b , e, op)
max_element(b , e)
max_element(b , e, op)
//
op 是函数,函数对象,一元谓词
#include #include // #include bool absLess(int elem1, int elem2) { return abs(elem1) < abs(elem2); } using namespace std; int main() { deque ideq; ideq.insert(ideq.end(), -12); for (int i = 2; i <= 8; i++) { ideq.insert(ideq.end(), i); } for (int i = -3; i <= 5; i++) { ideq.insert(ideq.end(), i); } for (deque::iterator iter = ideq.begin(); iter != ideq.end(); iter++) { cout << *iter << ' '; } cout << endl; deque::iterator result = min_element(ideq.begin(), ideq.end()); cout << "最小值:" << *result << endl; cout << "最大值:" << *max_element(ideq.begin(), ideq.end()) << endl; cout << "绝对值最小值:" << *min_element(ideq.begin(), ideq.end(), absLess) << endl; cout << "绝对值最大值:" << *max_element(ideq.begin(), ideq.end(), absLess) << endl; // system("pause"); return 0; }
上一篇: es6中reduce的基本使用方法