architecture - Architectural C++/STL question about iterator usage for O(1) list removal by external systems -
This is a very simple architectural question, though it is nervous to me for ages.
The point of using a whole list, for me anyway, is that o (1) insert / remove one (o) (1) withdrawal is the only way to erase an Iterator ( ). The only way to get an Iterator is to keep it from the initial insert () or to run it again.
So, what is to go around? An Iterator or Pointer?
It seems that if it is necessary for fast removal, such as a large list of types which are changing very often, then you should go to an Iterator and if you
< P> Here is a special cut-down example:In this example, we call some type of foo Fu is likely to be a base class pointer, but it is not here for simplicity.
Then we have a Fumelar, which holds a list of shared_ptr, FooPtr, that is responsible after the manager is liable for the life of the object.
Now, what is the return from addFoo ()? If I return the filter, then I can never remove it from the list in o (1), because I have to get it in the list. If I return a std :: list :: iterator, FooPtrListIterator, then wherever I need to remove the Futur I, just by derendering the iterator.
In this example, I have an example of a foo which can kill itself in some circumstances, Fu: Marvajmentation ().
Imagine some fu which is a timer which is below 0, which requires him to ask the manager to remove himself. The problem is that 'this' is a nude foo, so the only way to remove yourself is to call Fumegar: Eresfu () with a raw pointer. Now the manager has to find the object pointer to get an Iterator so that it can be removed from the list and deleted.
The only way around it is to store the itater in the object. I.e Foo is a FooPtrListIterator as the member variable.
struct Foo; Typedef boost :: shared_ptr & lt; Foo & gt; FooPtr; Typedef std :: list & lt; FooPtr & gt; FooPtrList; Typedef FooPtrList :: Iterator FooPtrListIterator; Structure FooManager {FooPtrList l; FooPtrListIterator addFoo (Foo * foo) {Return L. Inert (L. Beigein), Futur (Foo)); } Zero Erasefu (phenistist easter fu) {l.erase (foo); } Zero Erase Fu (Foo * Fu) {For (Foistist List Iterator This = L. Beigeine), ITE = Lend (It! = IT; ++ This) {If ((* this) .get () == Foo) {eraseFoo (this); Return; }} Emphasize ("Foo was not found!"); }}; FooManager g_fm; Struct Foo {int _v; Fu (Int V): _V (V) {} ~ Foo () {printf ("~ Foo% d \ n", _v); } Zero print () {printf ("% d \ n", _v); } After killing zero, conditionmate () {// something that will eventually kill this object, such as timer g_fm.eraseFoo (this); }}; Zero print list (FooPtrList & amp; l) {printf ("- \ n"); (For FooPtrListIterator = l.begin (), ite = l.end (); it! = Ite; ++ it) {(* it) - & gt; Print (); }} Zero test2 () {FooPtrListIterator it1 = g_fm.addFoo (new Foo (1)); PrintList (g_fm.l); FooPtrListIterator it2 = g_fm.addFoo (new Foo (2)); PrintList (g_fm.l); FooPtrListIterator it3 = g_fm.addFoo (new foo (3)); PrintList (g_fm.l); (* It2) - & gt; KillWhenConditionMet (); PrintList (g_fm.l); } So, I have questions: 1. If an object has to delete itself or some other system delete it, then in O (1), I have an object inside an object Iterator for? If so, is there anybody available to do an Iterator with invalid containers due to any other containers?
-
What is another way to do this?
-
According to any question, 'Push *' STL does not return the itater as a result of the container operation, which means that someone has to resort to 'insert *' is.
>
Please, no answer that says "do not pre-optimize", it makes me crazy. ;) This is an architectural question.
A problem that I store the iterator in the object that you remove the object from another iterator Be careful, because your object district does not know where it was destroyed, so that you can eliminate an illegal errator in the destroyer.
The reason for this is that Push * does not return an iterator that it is the inverse of pop *, so that you can treat your container as a stack, qi, or deck.
Comments
Post a Comment