【发布时间】:2017-03-02 17:57:05
【问题描述】:
所以我开始学习向量,我想从结构向量中删除一个元素,我有这个作为结构:
typedef struct Carro{
int id, cc, cv;
char marca[50], modelo[50];
}car;
typedef struct Condutor{
vector<car> cars;
int id;
int totalC=0;
char nome[50];
}driver;
这个要删除:
for(int i=0; i< (*ptr).size(); i++){
if((*ptr)[i].id == id){
(*ptr).erase((*ptr).begin +i);
verif=true;
break;
}
else{
verif=false;
}
}
但它似乎不起作用,因为我在尝试运行它时在擦除行中收到此错误:
invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator+'
如何从向量中删除元素?
【问题讨论】:
-
[OT] 你是来自 C 的偶然吗?您的代码中有几个 C'isms,您不必在 C++ 中执行。
-
甚至在 C 中你也可以说
ptr->thing -
begin() 是一个方法吗?
-
该特定错误可能是由您编写
begin + i而不是begin() + i引起的,但除非您发布 MCVE,否则我们无法判断。我们没有水晶球,也不知道您的ptr。