Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I erase a node in the middle of a std::queue?

Tags:

c++

containers

If I have a std:queue can I erase an element in the middle?

Or should I just go for a simple vector?

EDIT:

In the end, my search became between std::list and std::deque. this post gives a nice comparison, although I am still a bit undecided.

On one side, since after deletion I won't be accessing more members (the operation finishes) I am not that concerned about iterator invalidation. On the other side, I will probably access (or search) elements one by one, so random access might not be that important...

like image 748
KansaiRobot Avatar asked May 11 '20 03:05

KansaiRobot


People also ask

Can we delete in middle of queue?

You can use an index that contain the position of each element, and after if you want delete an element you use his position v(i). There are a lot of method, dependig by your algorithms. If you need to perform operations on elements in the middle, you need a List, not a Queue.

How do I remove a specific element from a queue in C++?

pop() function is used to remove an element from the front of the queue(oldest element in the queue). This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <queue> header file. The element is removed from the queue container and the size of the queue is decreased by 1.

How do I remove a particular element from a priority queue?

The remove() method of PriorityQueue class removes a single instance of the specified element from this queue, only if it is present.


1 Answers

  1. No, not unless you want to take all the elements out and put them back in again.

  2. A std::list might be better, but it depends on what else you want to do with it.

like image 84
Beta Avatar answered Sep 18 '22 06:09

Beta