我使用的std :: string ::迭代器在C ++不好受。此代码编译罚款(仍然没有得到正确的输出,但是这是我的错:TODO,修正算法)的开发 - C ++,我没有得到运行时错误。该错误是用Visual Studio速成2008 C ++,其中我收到指向一个错误:“表达式:字符串迭代不dereferencable,”和点到线的文件112

我调试告诉我,我可能会尝试取消引用过去的整句输入结束,但我看不到的地方。任何人都可以阐明一些?

std::string wordWrap(std::string sentence, int width)
{    
    std::string::iterator it = sentence.begin();

    //remember how long next word is
    int nextWordLength = 0;
    int distanceFromWidth = width;

    while (it < sentence.end())
    {
       while (*it != ' ' && it != sentence.end())
       {
          nextWordLength++;
          distanceFromWidth--;
          it++;
       }

       if (nextWordLength > distanceFromWidth)
       {
          *it = '\n';
          distanceFromWidth = width;
          nextWordLength = 0;
       }

       //skip the space
       it++;

   }

   return sentence;    
}
有帮助吗?

解决方案

首先,使用运算符=()上迭代,不操作者<():

while (it != sentence.end())

其次,这是向后:while (*it != ' ' && it != sentence.end())

您做一些与迭代器,比检查迭代器是否有效。相反,你应该检查是否是有效的第一:

while (it != sentence.end() && *it != ' ')

第三,你应该使用了迭代器++ ++迭代器,虽然这是不相关的崩溃。


四,主要问题是在这里:

*it = '\n';

由于在前述检查,while (it != sentence.end()的,这是可能达到这一迭代解引用而被在末端。甲解决将是做到这一点:

if (it != sentence.end() && nextWordLength > distanceFromWidth)

所以,现在,如果你已经达到了目的,你停下来。


固定上期后,现在唯一的问题是这样的:

//skip the space
++it;

此假定您正在跳过字符实际上是一个空间。但是关于字符串的到底是什么?以该字符串运行此功能:

"a test string " // <- space at end

和它会成功;它跳过的空间中,把迭代在end(),退出循环和成功。

然而,如果没有空间,它会崩溃,因为你已经达到了目的,并跳过过去的结束。要修正,添加一个检查:

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

在此最后的代码得到的:

std::string wordWrap(std::string sentence, int width)
{    
    std::string::iterator it = sentence.begin();

    //remember how long next word is
    int nextWordLength = 0;
    int distanceFromWidth = width;

    while (it != sentence.end())
    {
        while (it != sentence.end() && *it != ' ')
        {
            nextWordLength++;
            distanceFromWidth--;
            ++it;
        }

        if (it != sentence.end() && nextWordLength > distanceFromWidth)
        {
            *it = '\n';
            distanceFromWidth = width;
            nextWordLength = 0;
        }

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

    }

    return sentence;    
}

您可能会注意到这似乎是它有很多冗余检查。这可以是固定的:

std::string wordWrap(std::string sentence, int width)
{    
    std::string::iterator it = sentence.begin();

    //remember how long next word is
    int nextWordLength = 0;
    int distanceFromWidth = width;

    while (it != sentence.end())
    {
        while (*it != ' ')
        {
            nextWordLength++;
            distanceFromWidth--;

            ++it;

            // check if done
            if (it == sentence.end())
            {
                return sentence;
            }
        }

        if (nextWordLength > distanceFromWidth)
        {
            *it = '\n';
            distanceFromWidth = width;
            nextWordLength = 0;
        }

        //skip the space
        ++it;
    }

    return sentence;    
}

希望帮助!

其他提示

while (*it != ' ' && it != sentence.end())

变为

while (it != sentence.end() && *it != ' ')

所以第二表达不如果第一如果为假。评价

   if (nextWordLength > distanceFromWidth)

应该可能改变到

   if (it == sentence.end())
         break;
   if (nextWordLength > distanceFromWidth)

几乎可以肯定你的错误的结果是:

*it = '\n';

由于在前面的while循环的停止的条件之一是:

it != sentence.end()

如果它== sentence.end(),则* it = '\ n' 不会飞

有更多的错误,但是这是造成当前的问题之一。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top