我如何根据某些值 erase/delete class 个对象的向量
How can I erase/delete vector of class objects according to some Values
P S :可能已经有人问过这个问题,但我尝试了很多,而且我没有将指针与向量一起使用。如果解决方案是这样,请在这里告诉我如何使用指针。
我的问题:我正在创建一个包含 Car
class 个实例的向量,并使用了 getter
和 setter
方法在其中检索和推送新记录。即使我也在编辑该记录,但 我不知道如何删除特定记录! 我已将代码放在我自己尝试的注释中。有人可以帮我从这个向量中 remove/erase class 的特定记录/实例吗?
提前致谢。
Car.cpp
#include "Car.h"
#include "global.h"
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
int cid =1;
string Name;
float Price;
//In this function I want to delete the records
void deleteCarVector( vector<Car>& newAllCar)
{
int id;
cout << "\n\t\t Please Enter the Id of Car to Delete Car Details : ";
cin >> id;
//replace (newAllCar.begin(), newAllCar.end(),"a","b");
unsigned int size = newAllCar.size();
for (unsigned int i = 0; i < size; i++)
{
if(newAllCar[i].getId() == id)
{
cout << "Current Car Name : "<<newAllCar[i].getName() << "\n";
// Here Exactly the problem!
// delete newAllCar[i];
// newAllCar.erase(newAllCar[i].newName);
// newAllCar.erase(remove(newAllCar.begin(),newAllCar.end(),newAllCar.at(i).getId()));
}
}
printCarVector(newAllCar);
cout << endl;
}
Even i'm editing that records also but i dont know how to delete
particular record??? I have putted the code in comments which i tried
by myself so if anybody knows please telll me how can i "remove/erase"
the particular record from vector class object?
您的问题本身就有答案:根据您提供的 key/id,您需要 Erase–remove idiom 从 std::vector < Car >
中删除 Car
对象。
carVec.erase(std::remove_if(carVec.begin(), carVec.end(), [&id_to_delete](const Car& ele)->bool
{
return ele.getnewId() == id_to_delete;
}), carVec.end());
如果您不想使用 lambda 函数,您可以执行如下操作
void deleteCarVector( vector<Car>& newAllCar)
{
int id;
cout<<" \n\t\t Please Enter the Id of Car to Delete Car Details : ";
cin>>id;
auto carItr = newAllCar.begin();
while(carItr != newAllCar.end())
{
if((*carItr)->getId==id)
{
delete *carItr;
newAllCar.erase(carItr);
break;
}
carItr++;
}
printCarVector(newAllCar);
cout << endl;
}
P S :可能已经有人问过这个问题,但我尝试了很多,而且我没有将指针与向量一起使用。如果解决方案是这样,请在这里告诉我如何使用指针。
我的问题:我正在创建一个包含 Car
class 个实例的向量,并使用了 getter
和 setter
方法在其中检索和推送新记录。即使我也在编辑该记录,但 我不知道如何删除特定记录! 我已将代码放在我自己尝试的注释中。有人可以帮我从这个向量中 remove/erase class 的特定记录/实例吗?
提前致谢。
Car.cpp
#include "Car.h"
#include "global.h"
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
int cid =1;
string Name;
float Price;
//In this function I want to delete the records
void deleteCarVector( vector<Car>& newAllCar)
{
int id;
cout << "\n\t\t Please Enter the Id of Car to Delete Car Details : ";
cin >> id;
//replace (newAllCar.begin(), newAllCar.end(),"a","b");
unsigned int size = newAllCar.size();
for (unsigned int i = 0; i < size; i++)
{
if(newAllCar[i].getId() == id)
{
cout << "Current Car Name : "<<newAllCar[i].getName() << "\n";
// Here Exactly the problem!
// delete newAllCar[i];
// newAllCar.erase(newAllCar[i].newName);
// newAllCar.erase(remove(newAllCar.begin(),newAllCar.end(),newAllCar.at(i).getId()));
}
}
printCarVector(newAllCar);
cout << endl;
}
Even i'm editing that records also but i dont know how to delete particular record??? I have putted the code in comments which i tried by myself so if anybody knows please telll me how can i "remove/erase" the particular record from vector class object?
您的问题本身就有答案:根据您提供的 key/id,您需要 Erase–remove idiom 从 std::vector < Car >
中删除 Car
对象。
carVec.erase(std::remove_if(carVec.begin(), carVec.end(), [&id_to_delete](const Car& ele)->bool
{
return ele.getnewId() == id_to_delete;
}), carVec.end());
如果您不想使用 lambda 函数,您可以执行如下操作
void deleteCarVector( vector<Car>& newAllCar)
{
int id;
cout<<" \n\t\t Please Enter the Id of Car to Delete Car Details : ";
cin>>id;
auto carItr = newAllCar.begin();
while(carItr != newAllCar.end())
{
if((*carItr)->getId==id)
{
delete *carItr;
newAllCar.erase(carItr);
break;
}
carItr++;
}
printCarVector(newAllCar);
cout << endl;
}