Convert pointer to iterator

I have 2 structures that point to each other

struct Person{
  string name;
  string born;
  int age;
  Id* p_id;
};

struct Id{
  string id_number;
  Person* p_person;
};

these structures are stored in two ponters vectors for those structures called vec_id and vec_person. I need a function that finds Person in vec_person and then removes the corresponding Id in the vec_id vector. My problem is converting p_id to a pointer.

example of my code:

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). 

thanks for helping the guys

0
source share
4 answers

"" ( ) . . std:: remove_if . , , , , .

+2

, & * it.

int (,...) :: iterator, :

  vector<int>::iterator it(...);
+2
auto p = std::equal_range( vec_person.begin(), vec_person.end(), Peter, gt );

if ( p.first != p.second )
{
   vec_id.erase( std::remove( vec_id.begin(), vec_id.end(), *p.first ), 
                 vec_id.end() );
}   
0

, :

static const int arr[] = {1, 2, 3, 4};
std::vector<int> vec (arr, arr+ sizeof(arr)/sizeof(arr[0]));
std::vector<int>::iterator itr = vec.begin();

Iterator to Pointer

char* ptr = (char*)&itr;    //cast iterator to pointer

itr = (std::vector<int>::iterator&)(*ptr);  //cast pointer back to iterator
0

All Articles