C++ 哈夫曼,堆栈溢出

C++ 哈夫曼,堆栈溢出,c++,C++,给定的带有注释部分的方法可以很好地工作,我尝试做递归而不是while循环 void decodeFile(ibstream& infile, Node* encodingTree, ostream& file) { // initializing map here. string code = ""; /*while (true) { code += integerToString(infile.readBit());

给定的带有注释部分的方法可以很好地工作,我尝试做递归而不是while循环

void decodeFile(ibstream& infile, Node* encodingTree, ostream& file) {
    // initializing map here.

    string code = "";
    /*while (true) {
        code += integerToString(infile.readBit());        
        if (map.containsKey(code)) {
            if (map[code] == PSEUDO_EOF) break;
            file.put(map[code]);
            code = "";
        }
    }*/
    bitToString(infile,file,code,map);
}
void位字符串(ibstream&infle、ostream&file、string&code、Map&Map){
代码+=整数排序(infle.readBit());
if(地图容器(代码)){
if(map[code]==PSEUDO_EOF)返回;
file.put(映射[代码]);
代码=”;
}
位字符串(填充、文件、代码、映射);
}
但是,通过这种递归,在大文件上会出现堆栈owerflow错误

Huffman Encoding.exe中0x621DFDE处未处理的异常:0xC00000FD: 堆栈溢出


函数嵌套是有限制的。如果您尝试函数中的ex.nest函数10000次,您将得到错误。可以使用递归进行哈夫曼解码,但需要将其与迭代(或第二次递归)相结合。您可能应该在找到正确的代码后重置递归,并迭代到下一个代码,直到EOF。

没有分支来决定是否继续递归,因此它将执行无限递归。将递归转换为循环。递归的深度有一个自然的限制——递归每一位输入肯定会超过这个限制。递归在代码中甚至没有任何特殊用途;在编码文本的末尾有PSEUFO_EOF char,它停止递归。因此,Igor最好将th代码放在while循环中?
while
loop,或
for(;)
loop,任何您认为更方便的方法。只是不是递归。
void bitToString(ibstream& infile, ostream& file,string& code,Map<string,ext_char>& map){      
    code += integerToString(infile.readBit());
    if (map.containsKey(code)) {
        if (map[code] == PSEUDO_EOF) return;
        file.put(map[code]);
        code="";
    }
    bitToString(infile,file,code,map);
}