换字功能 嘿,我写了一个WordPress函数来在C++中格式化控制台文本。我的问题是A)我不清楚std::string::iterators到底做什么,或者B)我的一个迭代器设置不正确。有人能解释一下这个代码失败的原因吗

换字功能 嘿,我写了一个WordPress函数来在C++中格式化控制台文本。我的问题是A)我不清楚std::string::iterators到底做什么,或者B)我的一个迭代器设置不正确。有人能解释一下这个代码失败的原因吗,c++,algorithm,word-wrap,C++,Algorithm,Word Wrap,顺便说一句:如果这涉及太多细节,我很抱歉。我不确定大多数程序员(我是一个新手)是否在他们的机器上安装了一个C++编译器。p> std::string wordWrap(std::string sentence, int width) { //this iterator is used to optimize code; could use array indice //iterates through sentence till end std::string::i

顺便说一句:如果这涉及太多细节,我很抱歉。我不确定大多数程序员(我是一个新手)是否在他们的机器上安装了一个C++编译器。p>
std::string wordWrap(std::string sentence, int width)
{    
   //this iterator is used to optimize code; could use array indice 
   //iterates through sentence till end 
   std::string::iterator it = sentence.begin();
   //this iterator = it when you reach a space; will place a newline here
   //if you reach width;
   std::string::iterator lastSpace = sentence.begin();

   int distanceToWidth = 0;

   while (it != sentence.end())
   {
      while (it != sentence.end() && distanceToWidth < width)
      {
         if (*it == ' ')
         {
           lastSpace = it;
         }

         distanceToWidth++;
         it++;
     }

     distanceToLength = 0;
     *lastSpace = '\n';   

      //skip the space
      if (it != sentence.end())
      {
         it++;
      }
   }

   return sentence;    
}

我的问题是,我在不适当的时候收到了新词。我经常听到新词,我看不出有什么明显的错误。我希望这个问题的独立人士(我在这个算法上花了几个小时,没有得到正确的结果是非常令人沮丧的)可以看看它。还有,有什么明显的优化技巧吗

从代码显示的内容来看,您的输出是正确的。你搞错的是算法。使用调试器找出实际发生的情况。

迭代程序从
语句的第一个字符开始:

//this iterator = it when you reach a space; will place a newline here
//if you reach width;
std::string::iterator lastSpace = sentence.begin();
当到达第五个字符“this is a…”(空格)时,内部while循环在识别当前字符为空格之前退出(因为
distanceToWidth==width
)。然后在位置
lastSpace
中插入一个换行符,该换行符仍然指向字符串的第一个字符。这样,“This”的“t”就丢失了

Next
distance towidth
重置为零,并追加另一个
width
字符,尽管该行未在当前位置拆分,但在前面有一些字符(在
lastSpace
)。因此,此行可能包含比预期更多的字符。在本例中,“is”仍然与“this”位于同一行,而它应该被包装到下一行

您可能需要:


  • 将内部while的条件更改为
    问题是单词this是4个字符,而您的换行符是4个字符。因此,它试图在将lastSpace设置为合理值之前进行包装

    从代码的角度来看:

    lastSpace points to the "t" from the beginning of "this"
    distanceToWidth=0
    iterator=this is
             ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=1
    iterator=this is
              ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=2
    iterator=this is
               ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=3;
    iterator=this is
                ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=4;
    iterator=this is
                 ^
    
    check if we should loop (distanceToWidth<4) NO! Because distanceToWidth equals four!
    
    We break out of the loop.
    
    Recall that lastSpace was never modified it still points to the first character in the string!
    now we set the "t" character from "this" to be a newline!!
    
    lastSpace从“this”开头指向“t”
    距离宽度=0
    迭代器=这是
    ^
    
    检查是否应该循环(distanceToWidth更新:这是我的最新代码,显示正确的输出。如果您再次阅读,请发表评论。很抱歉格式不正确,但在每行前面添加四个空格很麻烦,现在是凌晨1:45

    std::string wordWrap(std::string sentence, int width)
    {    
    //this iterator is used to optimize code; could use array indice 
    //iterates through sentence till end 
    std::string::iterator it = sentence.begin();
    //this iterator = it when you reach a space; will place a newline here
    //if you reach width; also kind of hackish (used instead of comparing to NULL)
    std::string::iterator lastSpace = sentence.begin();
    
    int distanceToWidth = 0;
    
    //used in rare instance that there is a space
    //at the end of a line
    bool endOfLine = false;
    
    while (it != sentence.end())
    {
       //TODO: possible to stop recomparing against .end()?
       while (it != sentence.end() && distanceToWidth <= width)
       {
          distanceToWidth++;
    
          if (*it == ' ')
          {
             lastSpace = it;
    
             //happens if there is a space after the last character
             if (width == distanceToWidth)
             {
                *lastSpace = '\n'; 
             }
          }
    
          ++it;
       }
    
       //happens when lastSpace did encounter a space
      //otherwise
       if (lastSpace != sentence.begin())
       {
          *lastSpace = '\n';   
       }       
    
       lastSpace = sentence.begin();
       distanceToWidth = 0;
       }
    
       return sentence;    
    }
    
    std::string换行字(std::string语句,int-width)
    {    
    //此迭代器用于优化代码;可以使用数组索引
    //重复句子直到结束
    std::string::iterator it=句子.begin();
    //此迭代器=当您到达一个空格时,它将在此处放置一个换行符
    //如果您达到宽度;也有点粗俗(用于代替比较NULL)
    std::string::iterator lastSpace=句子.begin();
    int distanceToWidth=0;
    //在极少数情况下使用,即存在空间
    //在一行的末尾
    bool endOfLine=假;
    while(it!=句子.end())
    {
    //TODO:是否可能停止对.end()重新比较?
    
    while(it!=句子.end()&&distance ToWidth Thank,这正是我所需要的。+1不,这个问题是针对我的迭代器问题的特定范围。你的回答在修复输出算法方面对我没有帮助。尽管如果需要,请随时关闭此选项,现在我有了答案。
    std::string::iterator lastSpace;
    ...
    if (lastSpace) {
       *lastSpace = '\n';
    }
    
    lastSpace points to the "t" from the beginning of "this"
    distanceToWidth=0
    iterator=this is
             ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=1
    iterator=this is
              ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=2
    iterator=this is
               ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=3;
    iterator=this is
                ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=4;
    iterator=this is
                 ^
    
    check if we should loop (distanceToWidth<4) NO! Because distanceToWidth equals four!
    
    We break out of the loop.
    
    Recall that lastSpace was never modified it still points to the first character in the string!
    now we set the "t" character from "this" to be a newline!!
    
    std::string wordWrap(std::string sentence, int width)
    {    
    //this iterator is used to optimize code; could use array indice 
    //iterates through sentence till end 
    std::string::iterator it = sentence.begin();
    //this iterator = it when you reach a space; will place a newline here
    //if you reach width; also kind of hackish (used instead of comparing to NULL)
    std::string::iterator lastSpace = sentence.begin();
    
    int distanceToWidth = 0;
    
    //used in rare instance that there is a space
    //at the end of a line
    bool endOfLine = false;
    
    while (it != sentence.end())
    {
       //TODO: possible to stop recomparing against .end()?
       while (it != sentence.end() && distanceToWidth <= width)
       {
          distanceToWidth++;
    
          if (*it == ' ')
          {
             lastSpace = it;
    
             //happens if there is a space after the last character
             if (width == distanceToWidth)
             {
                *lastSpace = '\n'; 
             }
          }
    
          ++it;
       }
    
       //happens when lastSpace did encounter a space
      //otherwise
       if (lastSpace != sentence.begin())
       {
          *lastSpace = '\n';   
       }       
    
       lastSpace = sentence.begin();
       distanceToWidth = 0;
       }
    
       return sentence;    
    }