且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于排列的 C++ 递归算法

更新时间:2022-06-27 01:10:17

您的问题似乎出在smallString"函数上.在该函数中,OutOfRange 用于 s[j].我把打印像

Your problem seems to be in the "smallString" function. In that function, OutOfRange is used in s[j]. I put print like

for(i=0,j=0;j<s.length();i++,j++)
{
    if(i==k){j++;}
      cout<<"smallString:: s ::"<<s<<'\t'<<"k::"<<k<<'\n';
    res.push_back(s[j]); //Problem is here...
}

现在输出打印就像

smallString:: s ::abc k::0

smallString:: s ::abc k::0

smallString:: s ::abc k::0

smallString:: s ::abc k::0

smallString:: s ::bc k::0

smallString:: s ::bc k::0

smallString:: s ::bc k::1

smallString:: s ::bc k::1

smallString:: s ::bc k::1

smallString:: s ::bc k::1

smallString::s ::b k::0

smallString:: s ::b k::0

smallString:: s ::b k::1

smallString:: s ::b k::1

smallString:: s ::b k::1..

smallString:: s ::b k::1 . .

因此,在某个时间点它是s ::b k::1",因此您要从字符串b"中选择位置 1 处的字符.

So, At one point of time it comes as "s ::b k::1" so you are selecting the character at the position 1 from the string "b".

String 基本上是一个从 0 到 (n-1) 的字符数组.对于字符串b",只有第 0 个位置具有字符b".但我们正在访问一个不存在的职位.

String is basically an char array which start from 0 to (n-1). For string "b" only 0th position is having character 'b'. but we are accessing an non-existing position.

所以它抛出错误并从这里开始连续循环.

So it throws error and start looping continuously from here.

解决您的问题:

for(i=0,j=0;j<s.length();i++,j++)
{
    if(i==k)
    {
        j++;
    }
    else
    {
        cout<<"smallString:: s ::"<<s<<'\t'<<"k::"<<k<<'\n';
        res.push_back(s[j]);
    }
}