managing objects in std::list

From: Glen Able (Glen.Able_at_gmail.com)
Date: 02/15/05


Date: Tue, 15 Feb 2005 09:30:38 -0000

Hi,

I have a collection of lists, something like this:

std::list<Obj*> lists[10];

Now I add an Obj to one of the lists:

Obj* gary;
lists[3].push_front(gary);

Then later on, the Obj wants to remove itself from whichever list it was put
in (it's only allowed to be in one list). What's the best way to do this
efficiently?

My best idea is: when adding an Obj to a list, the Obj has to store the
address of the list, and also an iterator to the current start of the list
(i.e. where it's just been added). Later it can use these to remove itself.

Anyone have anything smarter than this?

ta,
G.A.



Relevant Pages

  • Re: Ansi Common Lisp chapter 3 exercise 6
    ... ;;lists should be represented by using the cdr to point to the first ... (defun gov-car (lst) ... (cons lst (cons obj nil))) ...
    (comp.lang.lisp)
  • Re: Dynamic list name from a string
    ... # no intermediary lists needed really. ... # make a sorted, by date, list of bears ... animals = [obj for obj in ns.Animals] ...
    (comp.lang.python)
  • Re: Method chaining
    ... This doesn't work with lists, as the methods return None rather than ... obj = getattr ... return selfie ... I can imagine it would be nice for the chained object to still be an instance of its original type. ...
    (comp.lang.python)
  • Re: Method chaining
    ... This doesn't work with lists, as the methods return None rather than ... The class needs to be designed with method chaining in mind before ... obj = getattr ...
    (comp.lang.python)
  • Method chaining
    ... This doesn't work with lists, as the methods return None rather than ... The class needs to be designed with method chaining in mind before ... obj = getattr ...
    (comp.lang.python)