Instead of maps, how to use a struct vector for the problem in C++?

Asked

Viewed 82 times

1

First I have a struct

strcuct Ficha{

      int resgistro;
      float pontuacao; 


 }Fichas[100];

is the following I will read random mode data, but a data always in the list will start with the number that is not integer on pointing

0.5//pontuação
89933//registro 
49494
0.4
87474
0.6
89044
88443
86965

The problem is the following 0.5 is of the two record that comes down, 0.4 is only 87474 and then comes 0.6 that belongs to the record that you have below the score this is the logic.

Good as data is in a file txt

Reading

void lerArquvivo(){
    ifstream Arquivo;
    Arquivo.open("Regeistro.txt");
    char linha[10];
    float n;
    while(Arquivo.getline(linha,10)){

        Arquivo >> n;


        separa(n);

        }
     Arquivo.close();
     }

Then create a function that separates integer from non-integer.

void separa(floar valor){
    int x = valor;
    float y = valor;
         if(x==y){
            //cout<<"Inteiro"<<endl;
                           }else{
                      //cout<<"Não e Inteiro"<<endl;
                            }
                        }

Here it comes, as I have to store in vector Chips Fichas[i].registroand Fichas[i].pontuacao, i can’t do to store in the numbers of the related vector position.

As an example of the problem.

posição do vetor      registro     pontuação
     0                   0            0.5
     1                  89487          0
     2                  78474          0
     3                    0            0.4
     .
     .......

1 answer

2


What you can do to complete the reading is as follows:

#include <string>
#include <sstream>
#include <ifstream>

// ...

ifstream file("Registro.txt");
float pontuacao;
std::string line;
while (std::getline(file, line)) {
    if (line.find('.') != std::string::npos) { // Se contém um ponto, é float
        std::stringstream(line) >> pontuacao;
    } else {
        int registro;
        std::stringstream(line) >> registro;
        // Inserir (pontuacao, registro) na sua tabela
    }
}

Note that here the score is read and persisted between the lines until a new score is found. And when a record number is read, it looked like the most recent score read.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.