molilover 2020-05-08 22:34 采纳率: 100%
浏览 141
已采纳

萌新刚入场,求大神看看到底是什么引起的错误

写的程序原先可以用,但现在出现了下面的错误,查了其他的帖,但看不太懂,希望有大神帮下忙

图片说明

以下是代码:

 #include<iostream>
 #include<vector>
 #include<string>
using namespace std;
class MyTreeNode
{
public:
    char date;
    float weight;
    bool isleaf;
    MyTreeNode *LNode;
    MyTreeNode *RNode;


    MyTreeNode(char date,float weight,bool isleaf)
    {
        this->date=date;
        this->weight=weight;
        this->isleaf=isleaf;
        this->LNode=NULL;
        this->RNode=NULL;       
    }

    void child(MyTreeNode *LNode,MyTreeNode *RNode)
    {
        this->LNode=LNode;
        this->RNode=RNode;
    }

};




MyTreeNode *create_h_tree(int count,char *nr,float *weight)
{
    vector<MyTreeNode*> forest;
    for(int i=0;i<count;i++)
    {
        MyTreeNode *temp=new MyTreeNode(nr[i],weight[i],true);
        forest.push_back(temp);
    }

    for(int i=0;i<count-1;i++)
    {
        MyTreeNode *A=NULL;
        MyTreeNode *B=NULL;
        int index=0;
        float min=999999;
        for(int j=0;j<count-1;j++)
        {
            MyTreeNode *temp=forest[j];
            if(temp->weight<min)
            {
                min=temp->weight;
                A=temp;
                index=j;
            }
    }
        forest.erase(forest.begin()+index);
        index=0;
        min=999999;
        for(int j=0;j<count-i-1;j++)
        {
            MyTreeNode *temp=forest[j];
            if(temp->weight<min)
            {
                min=temp->weight;
                B=temp;
                index=j;
            }
        }
        forest.erase(forest.begin()+index);
        MyTreeNode *newTree=new MyTreeNode('#',A->weight+B->weight,false);
        newTree->LNode=A;
        newTree->RNode=B;
        forest.push_back(newTree);
    }
    return forest[0];
}



void gencode(MyTreeNode *root,string code)
{
    if(root==NULL)
        return;
    if(root->isleaf)
    {
        cout<<root->date<<":"<<code<<endl;
    }
    else
    {
        gencode(root->LNode,code+"0");
        gencode(root->RNode,code+"1");
    }
}


void decode(MyTreeNode *root,int count,int *code)
{
    MyTreeNode *curr=root;
    int i=0;
    while(i<count)
    {
        if(curr->isleaf)
        {
            cout<<curr->date;
            curr=root;
            continue;
        }
        int c=code[i];
        i++;
        if(c==0)
        {
            curr=curr->LNode;
        }
        else
        {
            curr=curr->RNode;
        }
    }
    cout<<curr->date<<endl;
}



int main()
{
    const int count=4;
    char nr[count]={'A','B','C','D'};
    float weight[count]={7,5,2,4};

    MyTreeNode *root=create_h_tree(count,nr,weight);
    cout<<"哈弗曼编码为:======================="<<endl;
    gencode(root,"");
    const int length=10;
    int code[length]={1,0,1,1,1,0,0,1,1,0};
    cout<<"=====================要解码的序列为:"<<endl;
    for(int i=0;i<length;i++)
    {
        cout<<code[i];
    }
    cout<<endl;
    cout<<"解码结果为:";
    decode(root,length,code);
    cout<<"====================================="<<endl;
    return 0;
}
  • 写回答

1条回答 默认 最新

  • weixin_42446592 2020-05-09 10:37
    关注

    ......
    for(int j=0;j {
    MyTreeNode *temp=forest[j];
    if(temp->weight {
    min=temp->weight;
    A=temp;
    index=j;
    }
    }
    //问题出在这里,forest的size通过这句在减,最后导致越界了,这个例子里,forest的的初始size为4,,但是当forest的size=2时,有个过程:j=2,sizieof(forest)=2,时候导致MyTreeNode *temp=forest[j]中的j为2,forest[2]越界;
    forest.erase(forest.begin()+index);
    ......

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体