cb27a_c++_STL_算法_最小值和最大值

cb27a_c++_STL_算法_最小值和最大值
min_element(b,e) b--begin(), e--end()
min_element(b,e,op). op:函数,函数对象,一元谓词。
max_element(b,e)
max_element(b,e,op)

bool absLess(int elem1, int elem2),返回bool,有两个参数,就是二元谓词

error C2661: “std::deque<int,std::allocator<_Ty>>::insert”: 没有重载函数接受 1 个参数
deque插入需要两个参数。 ideq.insert(ideq.end(),-12);

 

 1 /*cb27a_c++_STL_算法_最小值和最大值
 2 min_element(b,e) b--begin(), e--end()
 3 min_element(b,e,op). op:函数,函数对象,一元谓词。
 4 max_element(b,e)
 5 max_element(b,e,op)
 6 
 7 bool absLess(int elem1, int elem2),返回bool,有两个参数,就是二元谓词
 8 
 9 error C2661: “std::deque<int,std::allocator<_Ty>>::insert”: 没有重载函数接受 1 个参数
10 deque插入需要两个参数。 ideq.insert(ideq.end(),-12);
11 */
12 
13 #include <iostream>
14 #include <algorithm>
15 #include <deque>
16 using namespace std;
17 
18 bool absLess(int elem1, int elem2)
19 {
20     return abs(elem1) < abs(elem2);
21 }
22 
23 int main()
24 {
25 
26     deque<int> ideq;
27     ideq.insert(ideq.end(),-12);
28     for (int i = 2; i <= 8; ++i)
29         ideq.insert(ideq.end(), i);
30     for (int i = -1; i <= 5; ++i)
31         ideq.insert(ideq.end(),i);
32     for (deque<int>::iterator iter = ideq.begin(); iter != ideq.end(); ++iter)
33         cout << *iter << ' ';
34     cout << endl;
35     cout << "min_element返回的是迭代器,所以输出需要解引用" << endl;
36     cout << "最小值: " << *min_element(ideq.begin(), ideq.end()) << endl;
37 
38     deque<int>::iterator result=min_element(ideq.begin(), ideq.end());
39     cout << "min_element算法_最小值: " << *result << endl;
40     cout << "最大值是: " << *max_element(ideq.begin(), ideq.end()) << endl;
41 
42     cout << "绝对最小值: " << *min_element(ideq.begin(), ideq.end(), absLess)<<endl;
43     cout << "绝对最大值: " << *max_element(ideq.begin(), ideq.end(), absLess) << endl;
44 
45     cout << "绝对最大值abs: " << abs(*max_element(ideq.begin(), ideq.end(), absLess)) << endl;
46     return 0;
47 }

 

posted @ 2020-02-19 13:27  txwtech  阅读(283)  评论(0编辑  收藏  举报