fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. template<typename T>
  8. T max2(const vector<T>& arr) {
  9. auto copy(arr);
  10. auto it = max_element(copy.begin(), copy.end());
  11.  
  12. swap(*it, *(copy.begin()));
  13. it = max_element(copy.begin() + 1, copy.end());
  14.  
  15. return copy[0] + *it;
  16. }
  17.  
  18. int main(int argc, char* argv[]) {
  19. cout << max2<double>({1.0, 2.0, 3, 4, 5}) << endl;
  20. return 0;
  21. }
  22.  
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
9