首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

deque使用出了有关问题~编译通过,但是不能访问

2012-03-02 
deque使用出了问题~编译通过,但是不能访问这是我写的一个关于霍夫曼树的类,构造函数用来建树加预存编码,声

deque使用出了问题~编译通过,但是不能访问
这是我写的一个关于霍夫曼树的类,构造函数用来建树加预存编码,声明了的char型deque却无法访问,请各位高手帮忙分析一下解决办法。

-------------------------------
HfmTree.h
-------------------------------

#include   <iostream>
#include   <deque>
#include   <vector>

namespace   HTree
{
enum   Henum
{
WeightArrNum   =   2^8,
MaxWeight   =   0xffff
};
}

class   HfmTree
{
public:
explicit   HfmTree(const   int   leaf,   const   int   weightArr[]);
virtual   ~HfmTree(void);
private:
struct   HfmNode
{
HfmNode()
:weight_(0)
,parent_(-1)
,lchild_(-1)
,rchild_(-1)
,used_(false)
{}
int   weight_;
int   parent_;
int   lchild_;
int   rchild_;
bool   used_;
};
typedef   ::std::vector <HfmNode>   NodeData;
typedef   ::std::vector <char   *>   CodeData;
CodeData   code_;
NodeData   node_;
int   leaf_;
int   head_;
};

-------------------------------
HfmTree.cpp
-------------------------------
#include   "hfmtree.h "

HfmTree::HfmTree(const   int   leaf,   const   int   weightArr[])
{
if   (leaf   <=   0)return;
                  HfmNode   new_node;
leaf_   =   leaf;
for   (int   s   =   0;   s   <   leaf_;   s++)
{
new_node.weight_   =   weightArr[s];
node_.push_back(new_node);
}
for   (int   i   =   0;   i   <   leaf_-1;   i++)
{
int   wa,   wb,   na,   nb;

wa   =   wb   =   HTree::MaxWeight;
for   (int   j   =   0;   j   <   leaf_;   j++)
{
if   (false   ==   node_[i].used_)
{
if   (node_[j].weight_   <   wa)
{
wb   =   wa;
nb   =   na;
wa   =   node_[j].weight_;
na   =   j;
}
else   if   (node_[j].weight_   <   wb)
{
wb   =   node_[j].weight_;
nb   =   j;
}
}
}
node_[na].parent_   =   leaf_   +   i;
node_[nb].parent_   =   leaf_   +   i;
node_[na].used_   =   true;
node_[nb].used_   =   true;
node_[leaf_   +   i].weight_   =   node_[na].weight_   +   node_[nb].weight_;
node_[leaf_   +   i].lchild_   =   na;
node_[leaf_   +   i].rchild_   =   nb;
}
head_   =   leaf_   *   2   -   2;

std::deque <   char   >   stack_;                                 //   从这行开始,deque声明却无法使用,一访问就报告内存访问出错误。

  std::cout   < <   stack_.size()   < <   std::endl;
for   (int   q   =   0;   q   <   leaf_;   q++)
{
int   k   =   q,   p;


while   (-1   !=   node_[k].parent_)
{
p   =   node_[k].parent_;
                                                if   (node_[p].lchild_   ==   k)
{
stack_.push_front( '0 ');
}
else
{
stack_.push_front( '1 ');
}
k   =   p;
int   stack_size   =   stack_.size();
code_[q]   =   new   char[stack_size   +   1];
for   (int   j   =   0;   j   <   stack_size;   j++)
{
code_[q][j]   =   stack_.front();
stack_.pop_front();
}
                                                      code_[q][stack_size]   =   0;
                                                      stack_.clear();
}
}
}

HfmTree::~HfmTree(void)
{
}


[解决办法]
和deque没关系,你这儿已经错了:node_[leaf_ + i].weight_ = node_[na].weight_ + node_[nb].weight_;
node_[leaf_ + i].lchild_ = na;
node_[leaf_ + i].rchild_ = nb;
leaf_ + i已经造成vector越界了。

热点排行