c++ - How to list the word with the highest score -


i'm trying find out how list word highest score, based on function calculate score, words come array of words, or so. how can tackle this?

since need word highest score, there no need keep track of scores all candidate words. keeping track of best 1 enough.

string best_word; int best_score = 0; (auto word &: all_the_words) {     int cur_score = scrabblescore(word);     if (cur_score > best_score) {         best_word = word;         best_score = cur_score;     } } // have best_word , best_score. 

edit: extend take care of words same best score.

vector<string> best_words; int best_score = 0; (auto word &: all_the_words) {     int cur_score = scrabblescore(word);     if (cur_score > best_score) {         best_words.clear();         best_words.push_back(word);         best_score = cur_score;     } else if (cur_score == best_score) {         best_words.push_back(word);     } } // have best_words , best_score. 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -