C++ 删除向量中的对象
C++ Deleting an object in a vector
我目前正在使用一个矢量来将人们固定在一个程序中。我正在尝试用
删除它
vectorname.erase(index);
我在函数中传递了向量,以及我要删除的元素。我的主要问题是如何在编译速度方面提高我的代码?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class person {
private:
string name;
public:
person() {}
person(string n):name(n){}
const string GetName() {return name;}
void SetName(string a) { name = a; }
};
void DeleteFromVector(vector<person>& listOfPeople,person target) {
for (vector<person>::iterator it = listOfPeople.begin();it != listOfPeople.end();++it) {//Error 2-4
if (it->GetName() == target.GetName()) {
listOfPeople.erase(it);
break;
}
}
}
int main(){
//first group of people
person player("Player"), assistant("Assistant"), janitor("Janitor"), old_professor("Old Professor");
//init of vector
vector<person> listOfPeople = { player, assistant, janitor, old_professor };
DeleteFromVector(listOfPeople, janitor);
}
无需定义index
,迭代器可用于访问vector中的对象:
for (vector<person>::iterator it = listOfPeople.begin(); it != listOfPeople.end(); ++it) {//Error 2-4
if (it->GetName() == target.GetName()) {
listOfPeople.erase(it);
break;
}
}
Since next line is to break for loop, we don't consider invalid iterator problem here.
您不需要该循环来从向量中删除对象。只需使用 std::find_if:
#include <algorithm>
//...
void DeleteFromVector(vector<person>& listOfPeople, const person& target)
{
// find the element
auto iter = std::find_if(listOfPeople.begin(), listOfPeople.end(),
[&](const person& p){return p.GetName() == target.GetName();});
// if found, erase it
if ( iter != listOfPeople.end())
listOfPeople.erase(iter);
}
listOfPeople.erase(
remove(listOfPeople(), listOfPeople.end(), target),
listOfPeople.end()
)
"remove" erase-remove idiom 中的操作会将除 target 之外的所有元素移动到向量范围的前面,"erase" 操作将删除末尾满足目标标准。这是非常有效的,即使它不像迭代版本那样具有表现力。
我目前正在使用一个矢量来将人们固定在一个程序中。我正在尝试用
删除它vectorname.erase(index);
我在函数中传递了向量,以及我要删除的元素。我的主要问题是如何在编译速度方面提高我的代码?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class person {
private:
string name;
public:
person() {}
person(string n):name(n){}
const string GetName() {return name;}
void SetName(string a) { name = a; }
};
void DeleteFromVector(vector<person>& listOfPeople,person target) {
for (vector<person>::iterator it = listOfPeople.begin();it != listOfPeople.end();++it) {//Error 2-4
if (it->GetName() == target.GetName()) {
listOfPeople.erase(it);
break;
}
}
}
int main(){
//first group of people
person player("Player"), assistant("Assistant"), janitor("Janitor"), old_professor("Old Professor");
//init of vector
vector<person> listOfPeople = { player, assistant, janitor, old_professor };
DeleteFromVector(listOfPeople, janitor);
}
无需定义index
,迭代器可用于访问vector中的对象:
for (vector<person>::iterator it = listOfPeople.begin(); it != listOfPeople.end(); ++it) {//Error 2-4
if (it->GetName() == target.GetName()) {
listOfPeople.erase(it);
break;
}
}
Since next line is to break for loop, we don't consider invalid iterator problem here.
您不需要该循环来从向量中删除对象。只需使用 std::find_if:
#include <algorithm>
//...
void DeleteFromVector(vector<person>& listOfPeople, const person& target)
{
// find the element
auto iter = std::find_if(listOfPeople.begin(), listOfPeople.end(),
[&](const person& p){return p.GetName() == target.GetName();});
// if found, erase it
if ( iter != listOfPeople.end())
listOfPeople.erase(iter);
}
listOfPeople.erase(
remove(listOfPeople(), listOfPeople.end(), target),
listOfPeople.end()
)
"remove" erase-remove idiom 中的操作会将除 target 之外的所有元素移动到向量范围的前面,"erase" 操作将删除末尾满足目标标准。这是非常有效的,即使它不像迭代版本那样具有表现力。