霍夫曼编码 c++
Huffman coding c++
所以我正在为一个项目做霍夫曼编码。但是,我的代码不起作用。当我在Visual Studio上运行它时,它没有给我一个错误。我试图做的是读取一个文件并将它们全部放入一个字符串中。并获取该字符串中每个字符的频率。但我认为当文件变得有点大时,我的代码似乎在无限循环中运行。谁能向我解释什么?顺便说一下,我有一个排序函数,我用它来按频率对节点*的向量进行排序。
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;
以下是我的排序函数
static struct {
bool operator()(NodeInterface* a, NodeInterface* b) {
if (a->getFrequency() == b->getFrequency()) {//if the frequencies are even,
if (b->getCharacter() == ' ') return false;
if (a->getCharacter() != ' ') {
return (int)a->getCharacter() < (int)b->getCharacter();
}
return false;
}
return a->getFrequency() < b->getFrequency();
}
} 分拣机;
我看到两个主要问题。
在 for 循环中有一个 for 循环,用于初始化和使用int i
更改内部循环的变量名称。
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
{
.
.
和分拣机结构。我会把它改写成这样。
static struct {
bool operator()(NodeInterface* a, NodeInterface* b) {
if (a->getFrequency() == b->getFrequency()) {//if the frequencies are even,
if (b->getCharacter() == ' ') return false;
if (a->getCharacter() == ' ') return true;
return (int)a->getCharacter() < (int)b->getCharacter();
}
return a->getFrequency() < b->getFrequency();
}
} sorter;
为您的 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;
}
相关文章:
- 如何确定我已使用非编码文件到达 EOF?
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 在卡萨布兰卡形成编码参数的列表
- 使用C++进行游程编码
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 使用已使用 java 编码的 openssl 解码数据
- boost 是否有按特殊类型值编码状态"compact optional"?
- 如何在CPP的给定目录中列出UTF编码的文件名?
- 如何使用 C++将 ISO-2022-KR 编码转换为 UTF-8 编码?
- 绝对编码器十六进制输入
- 防止C++中的硬编码数字
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 使用文件内容作为硬编码字符串
- 运行长度编码程序 c++ 的问题
- 以C++为单位进行运行长度编码
- *新的编码器*格式和运算符
- 在CRC-16 CCITT中将数据从二进制解码为文本,我应该输入一个码字,使用CRC生成器进行编码
- 使用 IMFSinkWriter 编码的视频的播放速度会根据宽度而变化
- C++和带有国家符号的文件路径(也许用 UTF8 编码)
- 仅在 zlib c++ 库中强制动态编码