Skip to content

Latest commit

 

History

History
26 lines (21 loc) · 615 Bytes

simple_cpp_argmax_argmin.md

File metadata and controls

26 lines (21 loc) · 615 Bytes

Simple C++ argmin/argmax for std::vector

template <typename T, typename A>
int arg_max(std::vector<T, A> const& vec) {
  return static_cast<int>(std::distance(vec.begin(), max_element(vec.begin(), vec.end())));
}

template <typename T, typename A>
int arg_min(std::vector<T, A> const& vec) {
  return static_cast<int>(std::distance(vec.begin(), min_element(vec.begin(), vec.end())));
}

Used as follows:

#include <vector>
#include <iostream>

void main() {
  std::vector<int> my_vec = { 1, 43, 2, 4, 2, 12 };
  int max_idx = arg_max(my_vec);
  std::cout << my_vec[max_idx] << std::endl;
}