fork(2) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <map>
  5.  
  6. unsigned short INVALID = 0x7FFF;
  7.  
  8. struct less
  9. {
  10. bool operator() (std::map<int, unsigned short>::value_type const& a, std::map<int, unsigned short>::value_type const& b)
  11. {
  12. if (a.second == INVALID || b.second == INVALID)
  13. return false;
  14. return a.second < b.second;
  15. }
  16. };
  17.  
  18. int main()
  19. {
  20. std::map<int, unsigned short> a;
  21. a[1] = INVALID;
  22. a[2] = 1;
  23. a[3] = 2;
  24. a[4] = 3;
  25. std::cout << "A max = " << std::max_element(a.begin(), a.end(), less())->second << std::endl;
  26. std::cout << "A min = " << std::min_element(a.begin(), a.end(), less())->second<< std::endl;
  27.  
  28. std::map<int, unsigned short> b;
  29. b[1] = 1;
  30. b[2] = 2;
  31. b[3] = 3;
  32. b[4] = INVALID;
  33. std::cout << "B max = " << std::max_element(b.begin(), b.end(), less())->second << std::endl;
  34. std::cout << "B min = " << std::min_element(b.begin(), b.end(), less())->second << std::endl;
  35.  
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
A max = 32767
A min = 32767
B max = 3
B min = 1