将指针转换为迭代器

use*_*307 2 c++ pointers iterator

我有两个结构体,它们互相指向

\n\n
struct Person{\n  string name;\n  string born;\n  int age;\n  Id* p_id;\n};\n\nstruct Id{\n  string id_number;\n  Person* p_person;\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

这些结构存储在两个结构向量中,称为 vec_id 和 vec_person。\n我需要在 vec_person 中查找 Person 的函数,然后删除向量 vec_id 中匹配的 Id。\n我的问题是将 p_id 转换为指针。

\n\n

我的代码示例:

\n\n
std::vector<Person*> vec_person;\nstd::vector<Id*> vec_id;\nvector <Person*>::iterator lowerb=std::lower_bound (vec_person.begin(), vec_person.end(), Peter, gt);\n//gt is matching function which is defined elsewhere\n//peter is existing instance of struct Person\n// lowerb is iterator, that works fine.\nvec_id.erase((*lowerb)->p_id);\n//gives error: no matching function for call to \xe2\x80\x98std::vector<Person*>::erase(Person*&)\xe2\x80\x99|\n//if i can convert pointer (*low)->pnumber to iterator, it would be solved(i guess). \n
Run Code Online (Sandbox Code Playgroud)\n\n

谢谢大家的帮助

\n

小智 6

要将迭代器转换it为指针,请使用表达式&*it

要将指向 int 右值的指针(例如 ... )转换为 a vector<int>::iterator,请使用以下声明:

  vector<int>::iterator it(...);
Run Code Online (Sandbox Code Playgroud)