功能给出错误的结果

use*_*982 2 c++ algorithm combinations operators

我已经尝试编写以下函数来通过从算法文本中转换算法来生成字符串的所有组合.但它会在输出中为所有组合打印整个字符串.

len = strlen(str);
for(i=0;i<pow(2,len);i++) 
{
        for(j=0;j<len;j++) 
        {
                if(i && (0x1 << j)) 
                {
                        cout<<str[j];
                }
        }
        cout<<endl;
}
Run Code Online (Sandbox Code Playgroud)

谢谢大家.

cod*_*ict 7

由于您要检查是否j在变量中设置了位,i您需要使用按位运算&符而不是逻辑&&:

if(i && (0x1 << j))
     ^^
Run Code Online (Sandbox Code Playgroud)