___ ___ erkimt help printing binary values of a Huffman algorithm ______ qstntxt ___

I'm making a Huffman tree and I want to show the result of the message on the screen. Something like:

%pre%

But I have problems saving the numbers to print them in order of the message.

%pre%

The problem is that when I save the numbers I can only do it with vector < int & Number and that generates the outputs:

%pre%

When I want to save it in a string or char, so that:

%pre%

    
______ azszpr39118 ___

The problem, as you say, is that you are working with sequences instead of numbers. The digits to the left are significant and that a whole type does not support it. Hence, you can not group each sequence into a single index of the vector %code% .

One solution is to replace %code% so that it happens to store elementso of type %code% :

%pre%

Now, for each letter, we convert the Boolean sequence into a string:

%pre%     
___

0

I'm making a Huffman tree and I want to show the result of the message on the screen. Something like:

Mensaje Original: Hola mundo
Huffman: 10010100010101010101

But I have problems saving the numbers to print them in order of the message.

/*
 *  Lo ideal sería que fuera vector<char*> Numero
 *  para que guardara las cosas algo así:
 *  Numero[] = { "1001", "001", "1111"....}
 *  enconces al mandar imprimir a Numero[0] saldría
 *  1001 en pantalla.
*/
vector<char> Letras;
vector<int> Numero;
cout << "Peso binario de cada letra" << endl;
for(CodigoHuffmanMap::const_iterator it = Bin.begin(); it != Bin.end(); ++it){
    // Imprimir y guardar la letra.     
    cout << it->first << " = ";
    Letras.push_back(it->first);
    // Imprimir el número. E.g. 1001
    copy(it->second.begin(), it->second.end(), ostream_iterator<bool>(cout));
    cout << endl;       
    // Guardar la cadena de números "1010" en vector<char*> Numero  
    copy(it->second.begin(), it->second.end(), back_inserter(Numero));  
}

cout << "Comprension" << endl;
for(int i = 0; i < strlen(EnunciadoPrueba); i++){
    for(int j = 0; j < Letras.size(); j++){
        if(Letras[j] == EnunciadoPrueba[i]){
            // Comprueba que está en orden imprimiendo Letras[j]
            // en lugar de Numero[j]
            cout << Numero[j];
            continue;
        }
    }
}   
cout << endl << "Todos los numeros que deberian salir en pantalla." << endl;
for(int i = 0; i < Numero.size(); i++)
    cout << Numero[i];

The problem is that when I save the numbers I can only do it with vector < int & Number and that generates the outputs:

cout << Numero[0]; // 1

When I want to save it in a string or char, so that:

cout << Numero[0];  // 10001

    
asked by akko 14.12.2016 в 12:10
source

1 answer

1

The problem, as you say, is that you are working with sequences instead of numbers. The digits to the left are significant and that a whole type does not support it. Hence, you can not group each sequence into a single index of the vector Numero .

One solution is to replace Numero so that it happens to store elementso of type string :

std::vector<std::string> Numero;

Now, for each letter, we convert the Boolean sequence into a string:

for(CodigoHuffmanMap::const_iterator it = Bin.begin(); it != Bin.end(); ++it){
    // ...
    std::string secuencia;
    for( bool val : it->second)
      secuencia.append(std::to_string(val));

    Numero.push_back(secuencia);
}
    
answered by 14.12.2016 / 12:31
source