[英]What does this line of code do?
void expand_combinations(const char *remaining_string, string const & s, int rema
in_depth)
{
if(remain_depth==0)
{
std::cout << s << std::endl;
return;
}
for(int k=0; k < strlen(remaining_string); ++k)
{
string str(s);
str.append(1, remaining_string[k]);
expand_combinations(remaining_string+k+1, str, remain_depth - 1); // what?
}
return;
}
On the call to the function, it's passing a string + an integer. 在调用该函数时,它将传递一个字符串+一个整数。 What does that become? 那会变成什么?
remaining_string is not a string; missing_string不是字符串; it's a pointer to a character. 它是指向字符的指针。 Therefore adding an integer to it simply moves the pointer. 因此,向其添加整数只会移动指针。
For example, if char *blah = "hello"
, then blah+1
would point to "ello"
. 例如,如果char *blah = "hello"
,则blah+1
将指向"ello"
。
It's passing a pointer to the k+1th character. 它正在传递一个指向第k + 1个字符的指针。 As it descends into the recursion, each call starts farther and farther into the string. 随着它递归递归,每个调用都越来越远地进入字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.