lundi 28 septembre 2020

How to define the value type for iterator which points to an element in another container in c++

The purpose I want to achieve is that: I have an unordered_map<int, list<int>::iterator> mp and a list<int> lst. How can I store the iterator of an element in lst. And later, by using the iterators in map, I can manipulate (e.g.,erase) the element in the list. (Assume free of iterator invalidation problem) The following snippet won't work:

unordered_map<int, list<int>::const_iterator> mp;
list<int> lst;
lst.push_back(1);
mp[1]=lst.crbegin(); //error here
lst.erase(mp[1]);
...

I have also tried to store raw pointers, but I was not able to erase an element in the list by using a raw pointer. I was wondering what is the best way to fulfill my purpose.

Aucun commentaire:

Enregistrer un commentaire