【问题标题】:size_t i < _str.length creates endless loop in c++size_t i < _str.length 在 C++ 中创建无限循环
【发布时间】:2017-04-07 08:32:49
【问题描述】:

我有一个类(数组),请参见下面的 ctor。我想创建方法 Array::read(_str) 来为 Array 的对象提供在接口中键入的数组。 (例如字符串 _str = "1 2 3")

为了确定字符串应该转换成的双精度数,我正在计算空格的数量。正确找到了空格,但循环不会在最后一个空格之后结束。 (见输出屏幕文本)。

为什么找到两个空格后循环没有结束??

ctor 数组

Array::Array(int _size)
{
    //ctor
    length = _size ;
    myArray = new double[length] ; // initialize array

    //default initialization
    for(size_t i = 0; i < length; i++)
    {
        myArray[i] = i ;
    }
}

方法数组::read(string _str)

void Array::read(string _str)
{
    // string_t find (<what to search>, <starting pos>) const ;

    // determine length (number of numbers)
    length = 0 ;
    int steps = 0 ;
    size_t i = 0 ;

    cout<<"Value of _str.length() : "<<_str.length() <<endl ; // test

    while( i < _str.length() && steps < 100)
    {

        // search for space starting it i
        i = _str.find(" ",i ) ;
        if(i!=string::npos) // npos is greatest possible size_t
            cout<<"_ found at: 1 =  "<< i <<endl ;

        length ++ ;     // new number present
        i ++ ;          // next time start after space
        steps ++ ;      // to prevent endless loop
    }
    cout<<endl<<steps ;

    delete[] myArray ; // free old array
    myArray = new double[length] ; // allocate space

    // fill with doubles


}

输出屏幕文本

Value of _str.length() : 5
_ found at: i = 1
_ found at: i = 3
_found at: i = 1
_found at: i = 3

这一直重复到 100 ,因此循环仅由步骤条件结束。

【问题讨论】:

  • 请告诉我们您如何使用这个Array 对象,最好创建一个Minimal, Complete, and Verifiable Example。此外,您显示的输出与您显示的代码不匹配。你期望什么输出?
  • > 有没有办法检查输入的_str是否真的包含数字?
  • std::stod (and friends) 函数可能是一个好的开始。可以在循环中用于从字符串中提取以空格分隔的数字,同时还可以验证 是否 是一个有效数字。

标签: c++ arrays while-loop size-t


【解决方案1】:

string::npos 被定义为size_t 的最大可能值。

const size_t npos = -1;

当您找不到字符时,i 等于 npos。然后添加一个,它会溢出,变成0

作为解决方案,试试这个:

if (i != string::npos) {
    // ...
    i++;
}

【讨论】:

    【解决方案2】:

    如果string::find 返回string::npos,则需要中断循环:

    while( i < _str.length() && steps < 100)
        {
    
            // search for space starting it i
            i = _str.find(" ",i ) ;
            if(  i==string::npos )
                break;
            else // npos is greatest possible size_t
                cout<<"_ found at: 1 =  "<< i <<endl ;
    
            length ++ ;     // new number present
            i ++ ;          // next time start after space
            steps ++ ;      // to prevent endless loop
        }
    

    【讨论】:

    • 也许使用else
    • 好吧,我的意思是if(i!=string::npos) => else
    • 哦,完全删除if(i!=string::npos)更干净
    【解决方案3】:

    我刚刚发现如果我将循环更改为:

    while( i < _str.length() && steps < 100)
        {
    
            // search for space starting it i
            i = _str.find(" ",i ) ;
            if(i!=string::npos) // npos is greatest possible size_t
            {
                cout<<"_ found at: 1 =  "<< i <<endl ;
                length ++;
                i ++ ;          // next time start after space
            }
    
    
            steps ++ ;      // to prevent endless loop
        }
    

    该函数确实给出了正确的结果。 (3 个步骤,找到 2 个空格) 感谢您的 quich 回复!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多