STL问题:这是O / T吗? [英] STL question: is this O/T ?
问题描述
伙计们,
我正在尝试编译这段代码来构建一个huffman树,但是我得到了一个
运行时错误,我的degugger结束了在STL文件中的某个地方。我做了什么?b $ b做错了。我很欣赏它不那么高效/风格等等,它只是
我想尝试的东西
谢谢
Mike
#include< assert.h>
#include< vector>
#include< list>
使用std :: list;
使用std :: vector;
class HuffmanNode
{
$ b $公开:
HuffmanNode * pLeft;
HuffmanNode * pRight;
char代表;
浮动频率;
HuffmanNode():pLeft(NULL),pRight(NULL),代表(-1),
频率(-1){}
void AddSubTreeToVector(vector< string>& v,string currentString);
};
int main()
{
list< HuffmanNode> (b i = 0; i <256; i ++)
b n.Represented = i;
n.frequency =(i * i * 10)%300;
nodes.push_back(n);
}
cout<< 人口来源清单: << nodes.size();
while(nodes.size()> 1)
{
cout<< ; " \ n\tListSize:" << nodes.size();
HuffmanNode newNode;
newNode.pLeft = new HuffmanNode;
newNode.pRight = new HuffmanNode;
list< HuffmanNode> :: iterator it;
list< HuffmanNode> :: iterator smallest = nodes.begin();
//找到最小的
for(it = nodes.begin(); it!= nodes.end(); it ++)
if(it->频率< =最小 - >频率)最小=它;
* newNode.pLeft = *(最小);
newNode.frequency =最小 - >频率;
nodes.erase(最小);
//找到下一个Snmallest
smallest = nodes.begin();
for(it = nodes.begin(); it!= nodes.end(); it ++)
if(it-> frequency< = smallest->频率)最小=它;
* newNode.pRight = *最小;
newNode.frequency + =最小 - >频率;
节点.erase(最小);
//添加新节点
nodes.push_back(newNode);
}
HuffmanNode hmt = *(nodes.begin());
//将树移动到矢量中进行编码:
vector< string> HuffmanCodes;
for(int i = 0; i< 256; i ++)
{
string str;
HuffmanCodes.push_back(str);
}
hmt.AddSubTreeToVector(HuffmanCodes,"");
for (int i = 0; i< 256; i ++)
{
string str = HuffmanCodes [i];
cout<< i<< : << str.c_str()<<结束;
}
系统(暂停);
}
/>
void HuffmanNode :: AddSubTreeToVector(vector< string>& v,string
currentString)
{
assert(如果(代表!= -1)v [代表] = currentString;
// LeftSubTree
if(pLeft)
{
string LeftString;
LeftString = currentString;
LeftString.append(" 0");
pLeft-> AddSubTreeToVector(v,LeftString);
}
// RightSubTree
if(pRight)
{
string RightString;
RightString = currentString;
RightString.append(" 0");
pRight-> AddSubTreeToVector(v,RightString);
}
}
Guys,
I''m trying to compile this code to build a huffman tree, but I''m getting a
runtime error and my degugger ends up somewhere in the STL files. What am i
doing wrong. I appreciate its not effiecient/stylistic, etc, it was just
something i wanted to try
Thanks
Mike
#include <assert.h>
#include <vector>
#include <list>
using std::list;
using std::vector;
class HuffmanNode
{
public:
HuffmanNode* pLeft;
HuffmanNode* pRight;
char Represented;
float frequency;
HuffmanNode() : pLeft(NULL), pRight(NULL), Represented(-1),
frequency(-1) {}
void AddSubTreeToVector(vector<string>& v, string currentString);
};
int main()
{
list<HuffmanNode> nodes;
for(int i=0;i<256;i++)
{
HuffmanNode n;
n.Represented = i;
n.frequency = (i*i *10 )% 300;
nodes.push_back(n);
}
cout << "Populated Source List: " << nodes.size();
while(nodes.size() >1)
{
cout << "\n\tListSize: " << nodes.size();
HuffmanNode newNode;
newNode.pLeft = new HuffmanNode;
newNode.pRight = new HuffmanNode;
list<HuffmanNode>::iterator it;
list<HuffmanNode>::iterator smallest = nodes.begin();
//Find Smallest
for(it = nodes.begin();it != nodes.end();it++)
if(it->frequency <= smallest->frequency) smallest = it;
*newNode.pLeft = *(smallest);
newNode.frequency = smallest->frequency;
nodes.erase(smallest);
//Find the next Snmallest
smallest = nodes.begin();
for(it = nodes.begin();it != nodes.end();it++)
if(it->frequency <= smallest->frequency) smallest = it;
*newNode.pRight = *smallest;
newNode.frequency += smallest->frequency;
nodes.erase(smallest);
//Add the New Node
nodes.push_back(newNode);
}
HuffmanNode hmt = *(nodes.begin());
//Move Tree into vector for encoding:
vector<string> HuffmanCodes;
for(int i=0;i<256;i++)
{
string str;
HuffmanCodes.push_back(str);
}
hmt.AddSubTreeToVector(HuffmanCodes,"");
for(int i=0;i<256;i++)
{
string str = HuffmanCodes[i];
cout << i << ": " << str.c_str() << endl;
}
system("PAUSE");
}
void HuffmanNode::AddSubTreeToVector(vector<string>& v, string
currentString)
{
assert(this);
if(Represented != -1) v[Represented] = currentString;
//LeftSubTree
if(pLeft)
{
string LeftString;
LeftString = currentString;
LeftString.append("0");
pLeft->AddSubTreeToVector(v,LeftString);
}
//RightSubTree
if(pRight)
{
string RightString;
RightString = currentString;
RightString.append("0");
pRight->AddSubTreeToVector(v,RightString);
}
}
推荐答案
Michael写道:
Michael wrote:
伙计们,
我正在尝试编译这段代码来构建一个霍夫曼树,但我m如果一个
运行时错误,我degugger在STL文件中的某个地方结束。我做错了什么。我很欣赏它不那么高效/风格等等,它只是我想要尝试的东西
谢谢
Mike
代码首先没有编译。
#include< assert.h>
#include< vector>
#include < list>
使用std :: list;
使用std :: vector;
#include< string>
#include< iostream>
#include< iterator>
#include< cstdlib>
使用std :: string;
使用std :: cout;
使用std :: endl;
类HuffmanNode
公共关系:
HuffmanNode * pLeft;
HuffmanNode * pRight;
char表示;
浮动频率;
HuffmanNode():pLeft(NULL),pRight(NULL),代表(-1) ,
frequency(-1){}
void AddSubTreeToVector(vector< string>& v,string currentString);
};
int main()
{
列表< Huffma nNode>节点;
for(int i = 0; i< 256; i ++)
//转到unsigned int或更好,size_t类型用于索引到
//向量。
{
HuffmanNode n;
n.Represented = i;
n.frequency =(i * i * 10)%300;
节点。 push_back(n);
}
cout<< 人口来源清单: << nodes.size();
while(nodes.size()> 1)
{
cout<< " \ n\tListSize:" << nodes.size();
HuffmanNode newNode;
newNode.pLeft = new HuffmanNode;
newNode.pRight = new HuffmanNode;
list< HuffmanNode> :: iterator它;
list< HuffmanNode> :: iterator smallest = nodes.begin();
//找到最小的
(it = nodes.begin();它!= nodes.end(); it ++)
if(it-> frequency< = smallest-> frequency)smallest = it;
* newNode.pLeft = *(最小);
newNode.frequency = smallest-> frequency;
nodes.erase(最小);
//找到下一个Snmallest
smallest = nodes.begin();
for(it = nodes.begin(); it!= nodes.end(); it ++)
if if(it-> frequency< = smallest-> frequency)smallest = it;
* newNode.pRight = *最小;
newNode.frequency + =最小 - >频率;
nodes.erase(最小);
//添加新节点
nodes.push_back(newNode);
haffmanNode hmt = *(nodes.begin());
//将树移动到矢量中进行编码:
vector< string> HuffmanCodes;
for(int i = 0; i< 256; i ++)
{
string str;
HuffmanCodes.push_back(str);
}
hmt.AddSubTreeToVector(HuffmanCodes,"");
for(int i = 0; i< 256; i ++)
{
string str = HuffmanCodes [i] ;
cout<< i<< : << str.c_str()<< endl;
}
system(PAUSE);
//尽量不要使用系统命令。这是一个单独的
主题,但许多程序员厌恶它,因为它影响了代码的可读性。
//哪里是main函数的return语句。
}
void HuffmanNode :: AddSubTreeToVector(vector< string>& v,string
currentString)
{
断言(this);
if(表示!= -1)v [代表] = currentString;
Guys,
I''m trying to compile this code to build a huffman tree, but I''m getting a
runtime error and my degugger ends up somewhere in the STL files. What am i
doing wrong. I appreciate its not effiecient/stylistic, etc, it was just
something i wanted to try
Thanks
Mike
The code didn''t compile in the first place.
#include <assert.h>
#include <vector>
#include <list>
using std::list;
using std::vector;
#include <string>
#include <iostream>
#include <iterator>
#include <cstdlib>
using std::string;
using std::cout;
using std::endl;
class HuffmanNode
{
public:
HuffmanNode* pLeft;
HuffmanNode* pRight;
char Represented;
float frequency;
HuffmanNode() : pLeft(NULL), pRight(NULL), Represented(-1),
frequency(-1) {}
void AddSubTreeToVector(vector<string>& v, string currentString);
};
int main()
{
list<HuffmanNode> nodes;
for(int i=0;i<256;i++) // Go for unsigned int or better, size_t type for indexing into
//the vector.
{
HuffmanNode n;
n.Represented = i;
n.frequency = (i*i *10 )% 300;
nodes.push_back(n);
}
cout << "Populated Source List: " << nodes.size();
while(nodes.size() >1)
{
cout << "\n\tListSize: " << nodes.size();
HuffmanNode newNode;
newNode.pLeft = new HuffmanNode;
newNode.pRight = new HuffmanNode;
list<HuffmanNode>::iterator it;
list<HuffmanNode>::iterator smallest = nodes.begin();
//Find Smallest
for(it = nodes.begin();it != nodes.end();it++)
if(it->frequency <= smallest->frequency) smallest = it;
*newNode.pLeft = *(smallest);
newNode.frequency = smallest->frequency;
nodes.erase(smallest);
//Find the next Snmallest
smallest = nodes.begin();
for(it = nodes.begin();it != nodes.end();it++)
if(it->frequency <= smallest->frequency) smallest = it;
*newNode.pRight = *smallest;
newNode.frequency += smallest->frequency;
nodes.erase(smallest);
//Add the New Node
nodes.push_back(newNode);
}
HuffmanNode hmt = *(nodes.begin());
//Move Tree into vector for encoding:
vector<string> HuffmanCodes;
for(int i=0;i<256;i++)
{
string str;
HuffmanCodes.push_back(str);
}
hmt.AddSubTreeToVector(HuffmanCodes,"");
for(int i=0;i<256;i++)
{
string str = HuffmanCodes[i];
cout << i << ": " << str.c_str() << endl;
}
system("PAUSE"); // As far as possible try not to use system command. It is a separate
topic, but many programmers loathe it since it affects readability of
the code.
// Where is the return statement for main function .
}
void HuffmanNode::AddSubTreeToVector(vector<string>& v, string
currentString)
{
assert(this);
if(Represented != -1) v[Represented] = currentString;
代表的类型碰巧是''char''!!将其更改为
size_t类型。不要进行如此危险的转换。然后在将相同的转换为unsigned int之后注意你的
编译器的警告。那个
应该有帮助。
-
Karthik
人类请''removeme_''查看我的真实电子邮件。
The type of Represented happens to be a ''char'' !! Change that to
size_t type. Do not make such dangerous conversions. Then heed your
compiler''s warnings after converting the same to unsigned int. That
should help.
--
Karthik
Humans please ''removeme_'' for my real email.
Karthik写道:
Karthik wrote:
代表的类型碰巧是''char ''!!将其更改为
size_t类型。不要进行如此危险的转换。然后在将相同的转换为unsigned int之后注意你的
编译器的警告。
The type of Represented happens to be a ''char'' !! Change that to
size_t type. Do not make such dangerous conversions. Then heed your
compiler''s warnings after converting the same to unsigned int.
哎呀!我的意思是size_t在这里,而不是unsigned int。
-
Karthik
人类请''removeme_''查看我的真实电子邮件。
Oops !! I meant size_t here , and not unsigned int.
--
Karthik
Humans please ''removeme_'' for my real email.
Karthik< re ******************* @ yahoo.com>写在
news:4092b08f
Karthik <re*******************@yahoo.com> wrote in
news:4092b08f
这篇关于STL问题:这是O / T吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!