c++ STL 最大值最小值
#include <iostream>
#include <algorithm>
#include <deque>
using namespace std;
//二元谓词
//比较两个数的大小
bool absLess(int elementParam1,int elementParam2)
{
return abs(elementParam1) < abs(elementParam2);
}
int main()
{
deque<int> deque1;
for (int k=0;k<10;k++)
{
deque1.insert(deque1.end(),k);
}
for (int k = -4; k<6; k++)
{
deque1.insert(deque1.end(), k);
}
deque<int>::iterator deque_iter;
for (deque_iter = deque1.begin();deque_iter != deque1.end();++deque_iter)
{
cout << *deque_iter << " ";
}
cout << endl;
//
deque<int>::iterator min_index = min_element(deque1.begin(), deque1.end());
cout << *min_index << endl;
int min_value = *min_element(deque1.begin(), deque1.end());
cout << min_value << endl;
deque<int>::iterator max_index = max_element(deque1.begin(),deque1.end());
cout << "max:" << *max_index << endl;
bool b1 = absLess(*min_index,*max_index);
cout << b1 << endl;
//绝对最小值
deque<int>::iterator min_index2 = min_element(deque1.begin(),deque1.end(),absLess);
cout << "绝对最小值:" << abs(*min_index2) << endl;
//绝对最大值
deque<int>::iterator max_index2 = max_element(deque1.begin(),deque1.end(), absLess);
cout << "绝对最大值:" << abs(*max_index2) << endl;
system("pause");
return 0;
}
============================
0 1 2 3 4 5 6 7 8 9 -4 -3 -2 -1 0 1 2 3 4 5
-4
-4
max:9
1
绝对最小值:0
绝对最大值:9
请按任意键继续. . .