There is a problem in the class method. It is necessary to rewrite the necessary class object from one list to another and delete it in one. But it is not fully working, I do not understand what the problem is.

Class Description:

class storage { // склад private: int size; provider provider; list <Medicine> medic; list <Medicine> sell_medic; public: storage(int size); void sell(); void show_storage(); void add(); void report(); ~storage(){ } }; 

Medecine Class Description:

 class Medicine {//лекарство public: string name; float price; string group_medicine; Manufacturer manufacturer; Medicine() { } Medicine(string name, string group_medecine, float price, Manufacturer manufacturer); ~Medicine() { } 

}; Contents list:

  Medicine("Ketorol Gel","Противовоспалительная Мазь", 62.7,Manufacturer("Dr.Reddy`s")), Medicine("Тизин","Капли для носа",43.15,Manufacturer("ФАРМАР ОРЛЕАН")), Medicine("Engystol","Противовирусное",205.1,Manufacturer("Хаймиттель")), Medicine("Stripsils intinsive","Противовоспальтельное",35.6,Manufacturer ("RIL")), Medicine("Уролесан","Противоспалительное",73.2,Manufacturer("ARTHERIUM")), Medicine("Цистон","Противовоспалительное",105.58,Manufacturer("Himalaya")), Medicine("Sinuforte","Спазморасширяющее",477.8,Manufacturer("Lab.Reig Jofre")), 

Description of the desired method:

 void storage::sell() { string sell_name; cout << "\nВведите имя продаваемого товара" << endl; cin >> sell_name; copy_if(medic.begin(), medic.end(), back_inserter(sell_medic), [sell_name](const Medicine& med) { return med.name == sell_name; }); auto result = remove_if(medic.begin(), medic.end(), [sell_name](const Medicine& med) { return med.name == sell_name; }); medic.erase(result, medic.end()); } 
  • one
    "But it is not fully working" - a meaningless set of words. Give a sane description of the problem. - AnT pm
  • @AnT This means that it does not comply with the logic that I laid. Because when checking the list <Medicine> sell_medic, it is still empty and it turns out that the method did not work - Norfo4ik 2:19 pm
  • So maybe it should be empty? If you do not initially have such a name in the source list, the search result will be empty. We do not see from here what you have there for the entrance. - AnT 2:22 pm
  • @AnT Why? I call the STL copy method with the predicate and it should copy by a certain criterion. And just delete. - Norfo4ik 2:24 pm
  • 3
    Well, as it is clear that your most likely operator == returns "not equal" where you expect equality. And because of what happens: the problem with the encoding, or you enter something wrong at all - we can't see from here. Try to start only Latin names. - AnT 3:53 pm

1 answer 1

You need only one line of code:

 sell_medic.splice(sell_medic.end(), medic, std::find(medic.begin(), medic.end(), sell_name)); 

To find an item in the list add to another list and remove from the first.