C++ 哈夫曼编码c++;
所以我正在为一个项目编写哈夫曼编码。然而,我的代码就是不起作用。当我在VisualStudio上运行它时,它没有给我一个错误。我试图做的是读取一个文件并将它们全部放入一个字符串中。并获取该字符串中每个字符的频率。但我认为当文件变得有点大时,我的代码似乎在无限循环中运行。谁能给我解释一下吗?顺便说一句,我有一个排序函数,我用它对节点*的向量按频率排序C++ 哈夫曼编码c++;,c++,C++,所以我正在为一个项目编写哈夫曼编码。然而,我的代码就是不起作用。当我在VisualStudio上运行它时,它没有给我一个错误。我试图做的是读取一个文件并将它们全部放入一个字符串中。并获取该字符串中每个字符的频率。但我认为当文件变得有点大时,我的代码似乎在无限循环中运行。谁能给我解释一下吗?顺便说一句,我有一个排序函数,我用它对节点*的向量按频率排序 ifstream infile; infile.open(filename); string q; string line; while (getl
ifstream infile;
infile.open(filename);
string q;
string line;
while (getline(infile, line))
{
q += line;
}
char y;
int count = 0;
int check = 0;
for (int i = 0; i < q.size(); i++) //if the string gets big, it seems to become an infinite loop in here
{
y = q[i];
for (int x = i - 1; x > 0; x--) //make sure not counting the same char
{
if (y == q[x])
{
check++;
}
}
if (check == 0)
{
for (int i = 0; i < q.size(); i++)
{
if (q[i] == y)
{
count++;
}
}
node*x = new node;
x->char1 = y; //my node have char
x->freq = count; //my node has frequency
list1.push_back(x);
}
count = 0;
check = 0;
}
sort(list1.begin(), list1.end(), sorter); //sort them from small to big
while (list1.size() > 1)
{
node*left = list1[0];
node*right = list1[1];
list1.erase(list1.begin(), list1.begin() + 2);
double sum = left->freq + right->freq;
node* x = new node;
x->freq = sum;
x->left = left;
x->right = right;
list1.push_back(x);
sort(list1.begin(), list1.end(), sorter);
}
list1.clear();
return true;
ifstream-infle;
infile.open(文件名);
串q;
弦线;
while(getline(infle,line))
{
q+=直线;
}
chary;
整数计数=0;
整数检查=0;
for(int i=0;i0;x--)//确保不计算相同的字符
{
如果(y==q[x])
{
check++;
}
}
如果(检查==0)
{
对于(int i=0;ichar1=y;//我的节点有char
x->freq=count;//我的节点有频率
列表1.推回(x);
}
计数=0;
检查=0;
}
排序(list1.begin(),list1.end(),sorter)//把它们从小到大分类
而(list1.size()>1)
{
node*left=list1[0];
node*right=list1[1];
list1.erase(list1.begin(),list1.begin()+2);
双和=左->频+右->频;
node*x=新节点;
x->freq=总和;
x->左=左;
x->右=右;
列表1.推回(x);
排序(list1.begin(),list1.end(),sorter);
}
清单1.clear();
返回true;
下面是我的排序函数
static struct {
bool operator()(NodeInterface* a, NodeInterface* b) {
if (a->getFrequency() == b->getFrequency()) {//if the frequencies are even,
if (b->getCharacter() == '\0') return false;
if (a->getCharacter() != '\0') {
return (int)a->getCharacter() < (int)b->getCharacter();
}
return false;
}
return a->getFrequency() < b->getFrequency();
}
静态结构{
bool运算符()(节点接口*a,节点接口*b){
如果(a->getFrequency()==b->getFrequency()){//如果频率是偶数,
如果(b->getCharacter()='\0')返回false;
如果(a->getCharacter()!='\0'){
返回(int)a->getCharacter()<(int)b->getCharacter();
}
返回false;
}
返回a->getFrequency()getFrequency();
}
}分拣机 我看到两个主要问题 在for循环中有一个for循环,用于初始化和使用
inti
更改内部循环的变量名
for (int i = 0; i < q.size(); i++) //if the string gets big, it seems to become an infinite loop in here
.
.
if (check == 0)
{
for (int i = 0; i < q.size(); i++) //Change this to int j for example
{
.
.
for(int i=0;i
还有分类器结构,我会把它改写成这样
static struct {
bool operator()(NodeInterface* a, NodeInterface* b) {
if (a->getFrequency() == b->getFrequency()) {//if the frequencies are even,
if (b->getCharacter() == '\0') return false;
if (a->getCharacter() == '\0') return true;
return (int)a->getCharacter() < (int)b->getCharacter();
}
return a->getFrequency() < b->getFrequency();
}
} sorter;
静态结构{
bool运算符()(节点接口*a,节点接口*b){
如果(a->getFrequency()==b->getFrequency()){//如果频率是偶数,
如果(b->getCharacter()='\0')返回false;
如果(a->getCharacter()='\0')返回true;
返回(int)a->getCharacter()<(int)b->getCharacter();
}
返回a->getFrequency()getFrequency();
}
}分拣机;
关于for循环的一些建议:
for (int i = 0; i < q.size(); i++) //if the string gets big, it seems to become an infinite loop in here
{
y = q[i];
//You can avoid this entire loop by using a structure like map
for (int x = i - 1; x > 0; x--) //make sure not counting the same char
{
if (y == q[x])
{
check++;
//break; //if you use a loop, break it once you find the character.
}
}
if (check == 0)
{
for (int j = 0; j < q.size(); j++)//Renamed variable + you can start this loop from j = i as you know there is no occurrence of y before that.
{
if (q[i] == y)
{
count++;
}
}
node*x = new node;
x->char1 = y; //my node have char
x->freq = count; //my node has frequency
list1.push_back(x);
}
count = 0;
check = 0;
}
for(int i=0;i0;x--)//确保不计算相同的字符
{
如果(y==q[x])
{
check++;
//break;//如果使用循环,请在找到角色后将其打断。
}
}
如果(检查==0)
{
对于(int j=0;jchar1=y;//我的节点有char
x->freq=count;//我的节点有频率
列表1.推回(x);
}
计数=0;
检查=0;
}
分拣机看起来是错误的;如果a->getFrequency()
和b->getFrequency()
是相等的,并且a->getCharacter()
和b->getCharacter()
都是\0
,那么a
和b
都是false
错误的。这将混淆排序和返回(int)a->getCharacter()<(int)b->getCharacter()
没有防护就足够了。@KenY-N如果a
和b
是等效的,那么a方法溢出,请尝试更改变量以便它们可以存储更多数据,例如int64\t无限循环问题很容易解决,只需使用F10逐行检查代码!并检查变量这是价值观!@aschepler啊……咖啡不够,或者是这样的借口!