紧凑、可读、高效的C++算法,可就地反转字符串的单词

Compact, readable, efficient C++ algorithm to reverse the words of a string IN-PLACE

本文关键字:字符串 单词 高效 可读 C++ 算法 紧凑      更新时间:2023-10-16

所以我试图为提示提出一个很好的C++解决方案

"字符串中的反转单词(单词由一个或多个单词分隔 空格)。现在就地执行。到目前为止,最受欢迎的字符串问题!

而我现在拥有的是一个怪物:

void reverse_words ( std::string & S )
{
/* 
    Programming interview question: "Reverse words in a string (words are separated by one or more spaces). Now do it in-place. By far the most popular string question!"
    http://maxnoy.com/interviews.html 
*/
    if (S.empty()) return;
    std::string::iterator ita = S.begin() , itb = S.end() - 1;
    while (ita != itb) 
    {
      if (*ita != ' ')
      {
         std::string sa; // string to hold the current leftmost sequence of non-whitespace characters
         std::string::iterator tempa = ita; // iterator to the beginning of sa within S
         while (ita != ' ' && ita != itb) sa.push_back(*ita++); // fill sa
         while (*itb == ' ' && itb != ita) --itb; // move itb back to the first non-whitespace character preceding it
         std::string sb; // string to hold the current rightmost sequence of non-whitespace characters
         std::string::iterator tempb = itb; // iterator to the end of sb within S
         while (*itb != ' ' && itb != ita) sb.push_back(*itb--); // fill sb
         S.replace(tempa, ita-tempa, sb); // replace the current leftmost string with the current rightmost one
         S.replace(tempb, itb-tempb, sa); // and vice-versa
       }
      else
      {
         ++ita; 
      }
    }   
}

我想我的想法是正确的(找到第一个字符串,将其与最后一个字符串交换,找到第一个字符串之后的下一个字符串,将其与最后一个字符串之前的字符串交换,等等),但我需要一些更好的工具来实现这一目标。如何利用标准库来解决这个问题?

如果您使用以下方法,这非常简单:

  1. 反转整个字符串。
  2. 分别颠倒每个单词。

这个算法已经是 O(n),你只需运行一次整个字符串,然后再次运行以找到每个单词的反向。

现在,为了使它更有效率,看看反转序列的算法,它从前面和后面读取一个,并将每个存储在另一个的位置。现在,每当你存储一个空格时,你只是终止了一个单词,所以现在就反转这个词。效率更高的原因是包含该字的缓存在 CPU 上仍然很热。这里的困难是在极端情况下做到这一点。您必须考虑中间单词,多个连续空格和字符串两端的空格。

我想就是这样。简单、高效、清晰。请注意,std::basic_string::find...pos >= str.size()时返回npos

void ReverseWords(std::string& msg) {
    std::string::size_type pos_a = 0, pos_b = -1;
    while ((pos_a = msg.find_first_not_of(' ', pos_b + 1)) != msg.npos) {
        pos_b = std::min(msg.find(' ', pos_a + 1), msg.size());
        std::reverse(msg.begin() + pos_a, msg.begin() + pos_b);
    }
    std::reverse(msg.begin(), msg.end());
}