C++ C++;对于_,每个循环可以';找不到指向的映射元素
我正在构建一个哈夫曼编码程序,当我尝试运行到目前为止的代码时,它只会给我一个警告:freq map object.begin()不存在 赫夫C++ C++;对于_,每个循环可以';找不到指向的映射元素,c++,stl,map,exc-bad-access,huffman-code,C++,Stl,Map,Exc Bad Access,Huffman Code,我正在构建一个哈夫曼编码程序,当我尝试运行到目前为止的代码时,它只会给我一个警告:freq map object.begin()不存在 赫夫 #ifndef HuffPuff_Huff_h #define HuffPuff_Huff_h //---Include--- #include <iostream> #include <vector> #include <set> using namespace std; //---Node--- struct No
#ifndef HuffPuff_Huff_h
#define HuffPuff_Huff_h
//---Include---
#include <iostream>
#include <vector>
#include <set>
using namespace std;
//---Node---
struct Node {
int weight;
char litteral;
string symbol;
Node* childL;
Node* childR;
void set_node(int w, char l, Node* L, Node* R){
weight = w;
litteral = l;
childL = L;
childR = R;
}
bool operator>(Node & r){
if(this->weight > r.weight)
return true;
return false;
}
};
//---Code---
struct Code {
string symbol;
char content;
};
//---HuffClass---
class Huff {
private:
typedef pair<char, int> c_pair;
vector<Code> code;
string content;
void copy_to(c_pair c);
public:
Huff(string);
~Huff();
string compress();
bool set_content();
string get_content();
string get_compress();
};
#endif
赫夫
//---Include---
#include <iostream>
#include <vector>
#include "Huff.h"
#include <map>
#include <set>
using namespace std;
//---|+ -|---
Huff::Huff(string c): content(c){}
Huff::~Huff(){}
//---Compress---
struct CopyTo {
vector<Node*>* & nodes;
CopyTo(vector<Node*>* & c):nodes(c){}
void operator()(pair<char, int> c){
Node * n = new Node;
n->set_node(c.second, c.first, NULL, NULL);
nodes->push_back(n);
}
};
void show_freq(pair<char, int> p) {
cout << p.first << "\t" << p.second << endl;
}
/*void show_freq(Node* p) {
cout << p->litteral << "\t" << p->weight << endl;
}*/
string Huff::compress(){
vector<Node *>* nodes; // Vector of nodes for later use
map<char, int>* freq = new map<char, int>; // Map to find weight of nodes
for(int i = 0; i < content.length(); i++)
(*freq)[content[i]]++;
for_each(freq->begin(), freq->end(), show_freq);
CopyTo copyto(nodes); //Copy map elements to nodes in this and next one
for_each(freq->begin(), freq->end(), copyto);
delete freq;
Node p;
while(nodes->size() != 1){ //Sorts nodes by weight and then removes two of them and replaces them with one
sort(nodes->begin(), nodes->end());
vector<Node *>::iterator beg = nodes->begin();
int w= (**beg).weight + (**beg++).weight;
Node* p = new Node;
p->set_node(w, '*', *nodes->begin(), *(nodes->begin()++));
nodes->erase(nodes->begin(), nodes->begin()+2);
nodes->push_back(p);
//for_each(nodes->begin(), nodes->end(), show_freq);
cout << "--------------" << endl;
}
Node* root = *nodes->begin();
return "110";
}
Main.cpp
int main(){
Huff mike("Testing-");
mike.compress();
}
/--Include---
#包括
#包括
#包括“Huff.h”
#包括
#包括
使用名称空间std;
//---|+ -|---
哈夫::哈夫(字符串c):内容(c){}
哈夫::~Huff(){}
//---压缩---
结构复制到{
向量*&节点;
CopyTo(vector*&c):节点(c){
void运算符()(对c){
Node*n=新节点;
n->set_节点(c.second,c.first,NULL,NULL);
节点->推回(n);
}
};
无效显示频率(对p){
cout begin(),nodes->end();
向量::迭代器beg=节点->开始();
INTW=(**beg).weight+(**beg++).weight;
Node*p=新节点;
p->set_节点(w,“*”,*节点->开始(),*(节点->开始()+);
节点->擦除(节点->开始(),节点->开始()+2);
节点->推回(p);
//对于每个节点(节点->开始(),节点->结束(),显示频率);
cout算法头的包含在哪里
结果
编译输出:
source.cpp:在成员函数“std::string Huff::compress()”中:
source.cpp:76:39:警告:有符号和无符号整数表达式之间的比较[-Wsign compare]
source.cpp:94:11:警告:未使用的变量“root”[-Wunused variable]
执行输出:
-1
t1
e1
G1
i 1
n1
s1
T 1 你张贴了这么多代码,我甚至看不到<>代码>每个调用!任何发布这个问题的机会,无关的-<代码>向量几乎都是一个坏主意。考虑使用<代码>矢量<代码>或<代码> Boo::pTrvector vector < /C>。