G
G
Gleb2020-05-30 21:29:14
C++ / C#
Gleb, 2020-05-30 21:29:14

Why are items not being removed from the list?

I want to create a list and load my own there, then I need to delete some element and insert a new one
why, what is up to l1.pop_front(); l1.pop_back(); What after is displayed on the screen the same thing?

list<Student> l1;
    fstream file{"some.txt"};

    Student report("Gosha",75);
    while (getString(file, report))
        l1.push_back(report);

    Student report1("Artem",100);
    getString(file, report1);
    l1.push_back(report1);

    Student report2("Dasha",85);
    getString(file, report2);
    l1.push_back(report2);

    report.print();
    report1.print();
    report2.print();

    l1.pop_front();
    l1.pop_back();

    report.print();
    report1.print();
    report2.print();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Zhilin, 2020-05-30
@shevzoom

When you do l1.push_back(report);, a copy is added to the list report. The content l1has nothing to do with report, report1, report2. Accordingly, when you change l1, naturally, the variables report*will not change.
How to make sure that copies are not created. Do l1.emplace_back();it (if necessary, pass parameters for the Student constructor there). Next: l1.back()will return you a link to the newly created student, and you can fill it in if necessary. (Be careful with the link, otherwise you will create a copy again :) )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question