在C++中仅获取类似的数据。向量

发布于 2025-01-22 07:31:12 字数 2294 浏览 4 评论 0原文

我正在尝试从具有与图像相似的最后值的.txt文件中获取所有数据。

例如,我经过并创建一个向量,其中仅保存在文件中,只有具有相同最后一个值的行“ 1565514F”,然后当我找到另一个最终值“ 1678721f”时,我创建了另一个文件和因此,在无限的情况下,第一个文件将具有“ 1565514f.txt”的名称,并且可以保存在同一或另一个向量中,像

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

另一个文件一样的数据将称为“ 1678721f.txt”,并且将类似的数据称为

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

i code the code the code。已经完成的是以下

// 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'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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2025-01-29 07:31:12

修改您的循环

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;
}

我尚未对此进行测试,因为我没有数据结构或文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文