Instead of maps how to use a struct vector for the C ++ problem? [duplicate]

1

First I have a struct

strcuct Ficha{

      int resgistro;
      float pontuacao; 


 }Fichas[100];

is the following I will randomly read data but a die always in the list will start with the number that is not integer bound

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

The problem is as follows: 0.5 is the two registers that come down, 0.4 is only 87474 and then comes 0.6 that belongs to the register that have below the score that is 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, as I have to store in the vector% wrapper% and% wrapper% I can not 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
     .
     .......
    
asked by anonymous 27.05.2015 / 00:51

1 answer

2

What you can do to finish 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 punctuation is read and persisted between the lines until a new punctuation is found. And when a record number is read, it would look like the most recent score read.

    
27.05.2015 / 13:23