Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数给出错误的结果_C++_Algorithm_Operators_Combinations - Fatal编程技术网

C++ 函数给出错误的结果

C++ 函数给出错误的结果,c++,algorithm,operators,combinations,C++,Algorithm,Operators,Combinations,我已尝试编写以下函数,通过从我的算法文本翻译算法来生成字符串的所有组合。但它会在所有组合的输出中保留打印整个字符串 len = strlen(str); for(i=0;i<pow(2,len);i++) { for(j=0;j<len;j++) { if(i && (0x1 << j)) { cout<

我已尝试编写以下函数,通过从我的算法文本翻译算法来生成字符串的所有组合。但它会在所有组合的输出中保留打印整个字符串

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;
}
len=strlen(str);

对于(i=0;i,由于要检查变量
i
中是否设置了
j
第位,因此需要使用逐位
&
运算符,而不是逻辑
&

if(i && (0x1 << j))
     ^^

<代码> >(i,(0x1,因为这是C++,更喜欢使用<代码> STD::String < /Cuth>)>代码> char */COD>。获得<代码> STD::String 比在<代码> char *<代码>中扫描<代码> 0”<代码>。此外, STD::String 更安全,并且可以增长。@ Thomas Matthews:你确实意识到(LeN-LeN)
不能代替
pow(2,len)
,对吗?@高炉:对不起,我的错误。混淆了参数。删除了关于
pow(2,len)==(len*len)
的评论。您会发现这很有用: