【发布时间】:2023-04-04 15:12:01
【问题描述】:
我有 2 个结构,它们相互指向
struct Person{
string name;
string born;
int age;
Id* p_id;
};
struct Id{
string id_number;
Person* p_person;
};
这些结构存储在两个向量中,这些结构称为 vec_id 和 vec_person。 我需要在 vec_person 中找到 Person,然后删除向量 vec_id 中匹配的 Id 的函数。 我的问题是将 p_id 转换为指针。
我的代码示例:
std::vector<Person*> vec_person;
std::vector<Id*> vec_id;
vector <Person*>::iterator lowerb=std::lower_bound (vec_person.begin(), vec_person.end(), Peter, gt);
//gt is matching function which is defined elsewhere
//peter is existing instance of struct Person
// lowerb is iterator, that works fine.
vec_id.erase((*lowerb)->p_id);
//gives error: no matching function for call to ‘std::vector<Person*>::erase(Person*&)’|
//if i can convert pointer (*low)->pnumber to iterator, it would be solved(i guess).
谢谢大家帮忙
【问题讨论】:
-
停止在容器中存储指针(除非你需要多态性——即使你有多态性,你也可以用一个类包装它或使用 std::shared_ptr/std_unique_ptr)
-
"我的问题是将 p_id 转换为指针。"但是
p_id已经在您的示例中是一个指针!