Advertisement
NickAndNick

Шаблоны: min, max

Dec 17th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement