NickAndNick

Шаблоны: min, max

Dec 17th, 2017
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3. template<typename Type, typename... Args>
  4. Type min(Args... args) {
  5.     auto seq = initializer_list<Type>({ args... });
  6.     auto mn = numeric_limits<Type>::max();
  7.     for (auto value : seq) if (value < mn) mn = value;
  8.     return mn;
  9. }
  10. template<typename Type, typename... Args>
  11. Type max(Args... args) {
  12.     auto seq = initializer_list<Type>({ args... });
  13.     auto mx = numeric_limits<Type>::min();
  14.     for (auto value : seq) if (value > mx) mx = value;
  15.     return mx;
  16. }
  17. int main() {
  18.     cout << ">>> ";
  19.     int a, b, c, d;
  20.     cin >> a >> b >> c >> d;
  21.     const auto mn = min<int>(a, b, c, d);
  22.     const auto mx = max<int>(a, b, c, d);
  23.     cout << "min: " << mn << "\nmax: " << mx << endl;
  24.     system("pause");
  25. }
RAW Paste Data