fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. template<class T>
  5. const T& Limit(const T& Min, const T& Max, const T& Value){
  6. const T& A = Min < Max ? Min : Max;//min
  7. const T& B = Min < Max ? Max : Min;//max
  8.  
  9. const T& C = std::max(Value, A);
  10. return std::min(C, B);
  11. }
  12.  
  13. int main(){
  14.  
  15. int Max = 100;
  16. int Min = -100;
  17. int Mid = 0;
  18.  
  19. std::cout << Limit(Max, Min, 101) << std::endl;
  20. std::cout << Limit(Max, Mid, 101) << std::endl;
  21. std::cout << Limit(Max, Min, 0) << std::endl;
  22. std::cout << Limit(Max, Mid, 50) << std::endl;
  23. std::cout << Limit(Max, Min, -101) << std::endl;
  24. std::cout << Limit(Max, Mid, -1) << std::endl;
  25. return 0;
  26. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
100
100
0
50
-100
0