Converting vector from char to string - C ++

0
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main () {
    string vet[10], aux;
    std::vector<char> name;
    int count=0, sum=0; 

    while (count<10) {
        getline(cin, aux);

        for (int i=0; i<aux.length(); i++)  name.push_back(aux[i]);

        for (int i=0; i<name.size(); i++) {
            name[i] = tolower(name[i]);                                 //para garantir que todos os caracteres estão em minúsculo  
            if (name[i] > 96 && name[i] < 123 && name[i] == 32)         //faixa de decimais das letras minúsculas e espaço em caso de nome composto
                sum += name[i];
        }

        char v[name.size()];                                            //vetor auxiliar criado para realizar a conversão de vetor de char para string

        for (int i=0; i<name.size(); i++)   {
            v[i] = name[i];
        }

        while (vet[sum%10] != "
vet[sum%10] = (string) v;
") //para evitar colisão sum++; vet[sum%10] = (string) v; //conversão para string e => K mod m = K % m em C++ cout << vet[sum%10] << endl; count++; sum = 0; for (int i=0; i<name.size(); i++) v[i] = '
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main () {
    string vet[10], aux;
    std::vector<char> name;
    int count=0, sum=0; 

    while (count<10) {
        getline(cin, aux);

        for (int i=0; i<aux.length(); i++)  name.push_back(aux[i]);

        for (int i=0; i<name.size(); i++) {
            name[i] = tolower(name[i]);                                 //para garantir que todos os caracteres estão em minúsculo  
            if (name[i] > 96 && name[i] < 123 && name[i] == 32)         //faixa de decimais das letras minúsculas e espaço em caso de nome composto
                sum += name[i];
        }

        char v[name.size()];                                            //vetor auxiliar criado para realizar a conversão de vetor de char para string

        for (int i=0; i<name.size(); i++)   {
            v[i] = name[i];
        }

        while (vet[sum%10] != "
vet[sum%10] = (string) v;
") //para evitar colisão sum++; vet[sum%10] = (string) v; //conversão para string e => K mod m = K % m em C++ cout << vet[sum%10] << endl; count++; sum = 0; for (int i=0; i<name.size(); i++) v[i] = '%pre%'; name.clear(); } cout << endl; for (int i=0; i<10; i++) cout << vet[i] << endl; return 0; }
'; name.clear(); } cout << endl; for (int i=0; i<10; i++) cout << vet[i] << endl; return 0; }

This code is using the Hashing concept to store names within a vector.

My question is:

  

Whenever I try to enter a name with 8, 16 or 24 characters, the   convert from a vector of characters to a string, the compiler   always places another 3 characters in front of the name. If I try   insert a name with 9, 17 or 25 characters, the compiler always places   other 2 characters in front of the name. If I try to enter a name   with 10, 18 or 26 characters, the compiler always puts another   character in front of the name.

     

Why does this happen? Is there any way to prevent this?

I need the names to be exactly (but in lowercase) how they were inserted into the input, but organized according to Hashing logic.

I have already debugged the code and saw that the problem is in the conversion from string to string, but I have not found a better way to do this.

%pre%

Thank you in advance!

    
asked by anonymous 22.02.2016 / 14:49

1 answer

2

1) Why do not you make direct name a char vector since you convert to it later? Also you do not have to copy beyond 10 positions, since you will ignore.

   char name[10];

2) You limit your name in the vet string to 10 positions, but try copying names with more characters, returning to the beginning. It looks like they are truncated characters appearing at the beginning.

As you comment that you have debugged, I imagine that v [i] has the correct string because you are not doing this truncation logic.

A better way to do this would be, after converting name to a char vector:

  snprintf(name, 10, "%s", aux.data());
  std::string buffAsStdStr = name;
    
22.02.2016 / 15:27