From 8a1efbaf0f8a064d6fffd9730b80f9fcf52d929f Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Sun, 4 Aug 2024 13:22:33 -0400 Subject: [PATCH] C++: Making the Grade: clean up methods --- cpp/making-the-grade/making_the_grade.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cpp/making-the-grade/making_the_grade.cpp b/cpp/making-the-grade/making_the_grade.cpp index eb60c55..b878111 100644 --- a/cpp/making-the-grade/making_the_grade.cpp +++ b/cpp/making-the-grade/making_the_grade.cpp @@ -30,7 +30,7 @@ std::vector above_threshold(std::vector student_scores, for (auto score : student_scores) { if (score >= threshold) - res.push_back(score); + res.emplace_back(score); } return res; @@ -55,11 +55,11 @@ student_ranking(std::vector student_scores, std::vector student_names) { std::vector res; - int rank = 1; + int len = student_scores.size(); - for (int i{0}; i < student_scores.size(); i++, rank++) { - res.push_back(std::to_string(rank) + "." + " " + student_names[i] + ": " + - std::to_string(student_scores[i])); + for (int i{0}; i < len; i++) { + res.emplace_back(std::to_string(i + 1) + "." + " " + student_names[i] + + ": " + std::to_string(student_scores[i])); } return res; @@ -70,7 +70,8 @@ student_ranking(std::vector student_scores, std::string perfect_score(std::vector student_scores, std::vector student_names) { - for (int i{0}; i < student_scores.size(); i++) { + int len = student_scores.size(); + for (int i{0}; i < len; i++) { if (student_scores[i] == 100) { return student_names[i]; }