c 2b 2b remove class from vector

Solutions on MaxInterview for c 2b 2b remove class from vector by the best coders in the world

showing results for - "c 2b 2b remove class from vector"
Elías
31 Jun 2020
1#include <algorithm>
2#include <vector>
3#include <iostream>
4
5class MyClass
6{
7    public:
8    std::string Name;
9};
10
11std::vector<MyClass> MyClass_Vector;
12
13int main()
14{
15    MyClass myclass_1;
16    MyClass myclass_2;
17    MyClass myclass_3;
18    MyClass myclass_4;
19    MyClass myclass_5;
20
21    myclass_1.Name = "Name_1";
22    myclass_2.Name = "Name_2";
23    myclass_3.Name = "Name_3";
24    myclass_4.Name = "Name_4";
25    myclass_5.Name = "Name_5";
26
27    MyClass_Vector.push_back(myclass_1);
28    MyClass_Vector.push_back(myclass_2);
29    MyClass_Vector.push_back(myclass_3);
30    MyClass_Vector.push_back(myclass_4);
31    MyClass_Vector.push_back(myclass_5);
32
33    std::cout << "Before " << MyClass_Vector.size() << "\n";
34    for(int i = 0; i < MyClass_Vector.size(); i++) std::cout << MyClass_Vector[i].Name << "\n";
35
36    std::string Name_Of_Class_To_Delete = "Name_2";
37
38    MyClass_Vector.erase(std::remove_if(MyClass_Vector.begin(), MyClass_Vector.end(), [&Name_Of_Class_To_Delete](const MyClass& myclass)->bool
39    {
40        return myclass.Name == Name_Of_Class_To_Delete;
41    }), MyClass_Vector.end());
42
43    std::cout << "\nAfter " << MyClass_Vector.size() << "\n";
44    for(int i = 0; i < MyClass_Vector.size(); i++) std::cout << MyClass_Vector[i].Name << "\n";
45
46    return 0;
47}
Santino
10 Nov 2017
1carVec.erase(std::remove_if(carVec.begin(), carVec.end(), [&id_to_delete](const Car& ele)->bool
2            {
3                return ele.getnewId() == id_to_delete;
4            }), carVec.end());
5