I'm trying to have a vector of iterators of a list, that will allow me to access or erase a list element in O(1). The use case is on one hand to use the list to define priority (in front() to have the most prioritized item while in back() to have the least prioritized item), but on the other hand, to get a direct access to every list node (so that I can move any item from the middle of the list to back() - for this I need to erase it from the middle of the list). I'm aware that this is probably not possible, but I really want to make the least changes to have the code running, instead of writing an awkward code (e.g. using "pair") to gain the same functionality. I've read posts like: Keeping vector of iterators of the data and Why can't I make a vector of references? And I understand that its probably not possible the way I want it, but I'm sure that its possible to do with least changes.
Here is a sample program:
#include <iostream>
#include <list>
#include <vector>
#include <functional>
using namespace std;
typedef struct _data
{
int d;
} tData;
typedef list <tData *> tListTest;
typedef vector <tListTest::iterator> tVecTest;
int main()
{
tListTest mylist;
tVecTest myvec;
tData *myData;
int i;
for (i=0; i<10; i++) {
cout << "Iteration " << i << endl;
cout << "Stage 1" << endl;
myData = new tData;
cout << "Stage 2" << endl;
myData->d = i;
cout << "Stage 3" << endl;
tListTest::iterator listIter = mylist.insert(mylist.end(), myData);
cout << "Stage 4" << endl;
myvec[i] = listIter;
cout << "Stage 5" << endl;
}
tListTest::iterator listIter = myvec[7];
cout << "Deleting " << (*listIter)->d << endl;
delete *listIter;
mylist.erase(listIter);
return 0;
}
here is the output:
Iteration 0
Stage 1
Stage 2
Stage 3
Stage 4
Segmentation fault (core dumped)
I've tried to change the vector definition to:
typedef vector <reference_wrapper<tListTest::iterator>> tVecTest;
And the assignment to:
myvec[i] = ref(listIter);
and it didn't help.
Any idea how can I make this code running? I'm pretty sure I'm almost there.
Thanks in advance