Page 1 of 1

Pointers to lists

Posted: 09 Dec 2005, 18:55
by krogothe
Say i declare two (or more) lists:

list<int> *things;
list<int> *stuff;

then fill them up with things and stuff(respectively)...

Now say i have a function that performs a loop on these lists, and to not need to write the function once for each list, i thought about creating a pointer to a list:

list<int> *pointertolist = things;

which gave me an AI exception (compiled no probs!)
How can i perform a loop thru a set number of lists?
also: are there any advantages to lists over vectors/deques?
finally: if i delete an element in the middle of the list/vector/deque, what happens?

thanks!

Posted: 09 Dec 2005, 20:10
by AF
hmm shouldnt there be a
thing = new list<int>;
somewhere?

if all else fails,

Code: Select all

list<int> a;
list<int>* b = &a;

Posted: 09 Dec 2005, 20:53
by krogothe
yes there is a
thing = new list<int>;
ill try the & , it might work...

Posted: 09 Dec 2005, 20:57
by AF

Code: Select all

list<int>* a;
list<int>* b;
a = 0;
a = new list<int>;
b = a;
if(b != 0){
   if(b->empty() != true){
      // code
   }
}
delete a;
b= 0;

Posted: 09 Dec 2005, 22:06
by Berion
yes there is a
thing = new list<int>;
ill try the & , it might work...
& olny returns the address of the variable.

Code: Select all

list<int*> a;
list<int*> *b = &a;
is the same as:

Code: Select all

list<int*> *a = new list<int*>;
list<int*> *b = a;
only that this time a and b are pointers

to search for and delete a list member:

Code: Select all

list<MyClass*> *mMyClassList = new list<MyClass*>;

...

MyClass *fMyClass = 0;

  for(list<MyClass*>::iterator i = mMyClassList->begin(); i !=  mMyClassList->end(); i++)
  {
    if((*i)->mID == pID)
    {
	 fMyClass = (*i);
    }
  }

  mMyClassList->remove(fMyClass);
MyClass is for example a unit, mMyClassList a list of the units and pID is the ID of a destroyed unit. Now you search for this unit and delete it.

Posted: 09 Dec 2005, 22:32
by krogothe
Thanks guys!
all sorted and working!