fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <stdexcept>
  5. #include <vector>
  6.  
  7. std::size_t FindIndexOfLargest(const std::vector <int> &input);
  8.  
  9. int main()
  10. {
  11. auto index = FindIndexOfLargest({4, 3, 30, 20, 12, 15});
  12. std::cout << index << "\n";
  13. }
  14.  
  15. std::size_t FindIndexOfLargest(const std::vector <int> &input)
  16. {
  17. if (input.empty()) {
  18. throw std::runtime_error("Error: Input vector was empty."); // or return an error value.
  19. }
  20.  
  21. std::vector<int>::const_iterator max_value = std::max_element(input.cbegin(), input.cend());
  22. return std::distance (input.cbegin(), max_value);
  23. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
2