2012-07-06 1 views
0

코딩 프로그램을 작성 중이며 코드를 실행했을 때 주파수 맵 객체가 .begin()이라는 경고 메시지가 표시됩니다. 존재하지 않습니다.C++ for_each 루프가 가리키는 요소를 찾을 수 없습니다.

Huff.h

#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 

Huff.cpp 알고리즘 헤더 포함되어

//---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(); 
} 
+1

1
내가 1
n은 1
의 1
t 나는 심지어'전화 for_each' 볼 수 없습니다! 문제가있는 [SSCCE] (http://sscce.org/)를 게시 할 수 있습니까? 무관계 -'벡터 '는 거의 항상 나쁜 생각입니다. 'vector >'또는'boost :: ptr_vector '을 대신 사용해보십시오. – Praetorian

답변

0

?

online compiler 결과

편집 출력 : source.cpp
: 멤버 함수 '표준 : 문자열 허프 :: 압축() : source.cpp
: 76 : 39 : 경고 : 비교 서명 부호없는 정수 표현식 사이에 [--Wsign 비교] source.cpp : 94 : 11 : 경고 : 사용되지 않는 변수 '루트'[-Wunused 변수]
실행 출력 :
- 1
T 1
,451,515,전자 1
g 당신은 너무 많은 코드를 게시 한 1