| 854 | typename F |
| 855 | > |
| 856 | auto min_scoring_element( |
| 857 | const T& container, |
| 858 | F score_func |
| 859 | ) -> decltype(std::make_pair(*container.begin(), 0.0)) |
| 860 | /*! |
| 861 | requires |
| 862 | - container has .begin() and .end(), allowing it to be enumerated. |
| 863 | - score_func() is a function that takes an element of the container and returns a double. |
| 864 | ensures |
| 865 | - This function finds the element of container that has the smallest score, |
| 866 | according to score_func(), and returns a std::pair containing that minimal |
| 867 | element along with the score. |
| 868 | - If the container is empty then make_pair(a default initialized object, infinity) is returned. |
| 869 | !*/ |
| 870 | { |
| 871 | double best_score = std::numeric_limits<double>::infinity(); |
| 872 | auto best_i = container.begin(); |
| 873 | for (auto i = container.begin(); i != container.end(); ++i) |
| 874 | { |
| 875 | auto score = score_func(*i); |
| 876 | if (score < best_score) |
| 877 | { |
| 878 | best_score = score; |
| 879 | best_i = i; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | using item_type = typename std::remove_reference<decltype(*best_i)>::type; |
| 884 | |
| 885 | if (best_i == container.end()) |
| 886 | return std::make_pair(item_type(), best_score); |
| 887 | else |
| 888 | return std::make_pair(*best_i, best_score); |
| 889 | } |
| 890 | |
| 891 | // ---------------------------------------------------------------------------------------- |
| 892 | |