I have a couple of list containing objects, along with a third list that refers to some of the elements in both of these lists. I want to be able to add and remove elements from the third list, without losing them from my first two lists. However, using vector erase(first, last) also calls the destructor of the objects, which I don't want it to do. Is there an alternative function that won't destroy the objects, or do I have to do it manaully?
The code below is my current approach, using the erase method, however it is also throwing a segmentation fault that I can't track down:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
std::vector<myObj1*> inputList;
std::vector<myObj2*> outputList; //myObj1 and myObj2 both extend myObj
std::vector<myObj*> linkedList; //even entries are input, odd entries are output
...
for(unsignedint i=0; i<numLinks; i++)
{
if(*linkedList[i*2] == *inputObj && *linkedList[i*2+1]==*outputObj)
{
linkedList.erase(linkedList.begin()+i*2, linkedList.begin()+i*2+2); // segmentation fault thrown by the erase function
inputObj->setLinked(false);
outputObj->setLinked(false);
returntrue;
}
}
Okay, I've modified my code so that it shifts the tail of the list up, rather than attempting to delete the entries that I no longer want in the list. This function returns succesfully, however when I go to print out the list I'm still getting a segmentation fault. I have tracked this down to the resize method resulting in a list of null elements, and have a similar problem when expanding the list size of the elements being scrambled. I've been struggling with pointers on this list, so any advice on how to resolve this issue would be greatly appreciated.
I have tried using vector<myObj**> linkedList, however when I print out the elements they are all scrambled again. Really don't understand why as I'm using this type successfully elsewhere.