codewars Reversed Words

include <string>
#include <vector>
std::string reverse_words(const std::string& str) {
  
  std::stringstream ss(str);
  std::vector<std::string>mp;
  std::string word;
  std::string result;
  
  while(ss>>word){
    mp.push_back(word);
  }
  std::reverse(std::begin(mp), std::end(mp));
  
  for(auto it = mp.begin();  it != mp.end(); ++it){
    //空格问题
    /*for (每个单词 it) {
    if (不是第一个) result += " ";
    result += *it;
}*/
    if(it != mp.begin()) result += " ";
    
    result += *it;
  }
    return result;
}
posted @ 2025-08-26 16:05  Civilight~Eterna  阅读(4)  评论(0)    收藏  举报