Николай Веселуха
Высший разум
(386927)
9 лет назад
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<int> test = { 7, 5, -4, 3, -11, 9, 2, 8 };
for (auto i : test) cout << setw(5) << i;
auto lambda = [](int a, int b) { return abs(a) < abs(b); };
int max = *max_element(test.begin(), test.end(), lambda);
int min = *min_element(test.begin(), test.end(), lambda);
cout
<< "\n Abs max: " << max
<< "\n Abs min: " << min
<< endl;
// Модули и сам как-нибудь сравнишь.
cin.get();
}
Капитан ГуглИскусственный Интеллект (146261)
9 лет назад
auto mm = minmax_element(test.begin(), test.end(),[](int a, int b) { return abs(a) < abs(b); });
cout << "\n Abs max: " << *mm.second << "\n Abs min: " <<*mm.first <<endl;
Как найти максимальный элемент по абсолютному значению, используя stl?