fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <iterator>
  4. #include <vector>
  5. #include <functional>
  6. using namespace std;
  7.  
  8. template<typename C,
  9. typename T = typename
  10. C::value_type>
  11. T third_max_val(const C& coll)
  12. {
  13. set<T, greater<T>> ret;
  14. for(const auto& el : coll)
  15. {
  16. ret.insert(el);
  17. if(ret.size() > 3u)
  18. ret.erase(prev(ret.end()));
  19. }
  20. return *ret.crbegin();
  21. }
  22.  
  23. int main() {
  24. vector<int> nums{1,4,9,3,2,8,7};
  25. cout << third_max_val(nums) << endl;
  26. return 0;
  27. }
Success #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
7