make the function below more optimized and cleaner std::set<std::string>…
Jump to turn (6)
- 1make the function below more optimized and cleaner std::set parseStringToWords(s
- 2explains what the function do
- 3do not use auto and alpha
- 4when i declare a string do i have to string.clear() to intiailized it?
- 5and i can just string += something correct
- 6is there anything wrong with this below? std::set parseStringToWords(string rawW
- User
make the function below more optimized and cleaner std::setstd::string parseStringToWords(string rawWords) { // complete function
code// parse the words and return a set of keywords set<std::string> keywords; rawWords = convToLower(rawWords); // in order to make the keywords case insensitive string current_word = ""; int last_index = 0; int last_length = 1; for(unsigned int i = 0; i < rawWords.length(); i++) { if(ispunct(rawWords[i]) || rawWords[i] == ' '){ // if the character is a punction, then segment off that string current_word = rawWords.substr(last_index, last_length-1); // now add that word to keywords if it has a length greater than 2 if(current_word.length() > 1) { keywords.insert(current_word); } last_length = 0; last_index = i+1; } last_length++; } // run one more time to check the final keyword current_word = rawWords.substr(last_index, last_length); if(current_word.length() > 1) { keywords.insert(current_word); } return keywords;