Home > Software engineering >  How do I delete a pointer in the queue pointer?
How do I delete a pointer in the queue pointer?

Time:09-16

Two Pointers queue element is the same, remove the pointer element in a queue, another queue in how to deal with? The pointer is not become NULL last q2!
 class A 
{
Public:
A (int A)
{
Id=a;
}
Int id=0;
};

Void main ()
{
DequeDequeA * A=new A (3);
A * b=new A (4);
Q1. The push_back (a);
Q1. The push_back (b);
Q2. The push_back (a);
Q2. The push_back (b);
For (auto & amp; P: q1)
{
delete p;
P=NULL;
}
For (auto & amp; P: q2)
{
If (p==NULL)
{
Printf (" NULL \ r \ n ");
}
The else
{
Printf (" % d \ r \ n ", p - & gt; Id);
}
}
}

CodePudding user response:

STL containers advice directly to store objects

CodePudding user response:

Record the index position, corresponding to delete

CodePudding user response:

refer to the second floor oyljerry response:
record index position, delete the corresponding

Now the question is not how to delete storage element,
But when I was in operating a queue, q2 operation, for example, don't know it to store Pointers is effective is not effective, if invalid, still directly using memory corruption,

CodePudding user response:

reference me_zyk reply: 3/f
Quote: refer to the second floor oyljerry response:

Record index position, corresponding to the deletion of

Now the question is not how to delete storage element,
But when I was in operating a queue, q2 operation, for example, don't know it to store Pointers is effective is not effective, if invalid, still use memory corruption, directly


Insert/delete/a for three places have to lock

CodePudding user response:

With smart Pointers
  • Related