'Get only similar data within a c++ vector

I'm trying to get all the data from a .txt file that have a similar last value, as in the image.

image file

for example, I go through and create a vector where I save in a file only the lines that have the same last value "1565514F" then when I find a different final value "1678721F" I create another file and so on infinitely, the first file would have the name "1565514F.txt" and would save in the same, or in another vector, data like

1001;2021-03-01;False;0;0;1565514F
1001;2021-03-02;False;0;0;1565514F
1002;2021-03-03;False;0;0;1565514F
1002;2021-03-04;False;0;0;1565514F
1003;2021-03-05;False;0;0;1565514F
1003;2021-03-06;False;0;0;1565514F
1004;2021-03-07;False;0;0;1565514F

another file would be called "1678721F.txt" and would have data like this

1006;2021-03-03;False;0;0;1678721F
1006;2021-03-04;False;0;0;1678721F
1001;2021-03-05;False;0;0;1678721F
1001;2021-03-06;False;0;0;1678721F
1004;2021-03-07;False;0;0;1678721F
1004;2021-03-08;False;0;0;1678721F
1003;2021-03-09;False;0;0;1678721F

the code that I have done is the following

// read file
ifstream archivoPlanificacion;
archivoPlanificacion.open("entrada/Planificacion.txt");
string linea;
vector<string> planificacionVector;
while(getline(archivoPlanificacion, linea))
{
    planificacionVector.push_back(linea);   
}
archivoPlanificacion.close();

for (int i = 0; i < planificacionVector.size(); i++) {
    vector<string> planificacionSplitted = splits(planificacionVector[i], ';');
    string planificacionFecha = planificacionSplitted[1];
    string planificacionRut = planificacionSplitted[5];

    cout << "Planificacion Rut: " << planificacionRut << endl;

    if (planificacionRut == planificacionSplitted[5]){
        vector<string> soloRutMismoUser;
        soloRutMismoUser.push_back(planificacionSplitted[5]);
        //imprimir vector soloRutMismoUser
        for (int i = 0; i < soloRutMismoUser.size(); i++) {
            cout << "soloRutMismoUser: " << soloRutMismoUser[i] << endl;
        }
        
    } else {
        cout << "nuevo rut" << endl;
        string aux = planificacionRut;
        //crear archivo de salida
    }

    cout << "Planificacion Fechas: " << planificacionFecha << endl;
}

I don't know if there is a better way, but I asked because I was nesting many for, I would appreciate any help, I still can't separate the data with the "if", thanks



Solution 1:[1]

Modifying your loop

string currentCol5; // current 'rut'
vector<string> sameCol5;  // collection of same 'rut' col5

for (int i = 0; i < planificacionVector.size(); i++) {
    vector<string> planificacionSplitted = splits(planificacionVector[i], ';');
    string planificacionFecha = planificacionSplitted[1];
    string planificacionRut = planificacionSplitted[5];

    cout << "Planificacion Rut: " << planificacionRut << endl;

    if (currentCol5 == planificacionSplitted[5] || currentCol5.length() == 0){
        sameCol5.push_back(planificacionSplitted[5]);
    } else {
      // write file here
      for (int i = 0; i < sameCol5.size(); i++) {
            cout << "soloRutMismoUser: " << sameCol5[i] << endl;
        }
       // start next set
       currentCol5 = planificacionSplitted[5];
       sameCol5.clear();
    }

    cout << "Planificacion Fechas: " << planificacionFecha << endl;
}

I have not tested any of this because I don't have the data structures or the file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pm100