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

Viewed 2194

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...

2 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.

std:: queue is a container adapter i.e. it processes elements in a specific order 'First in First Out'. Why would you want to break this order? First, you can't erase elements from any position of an std:: queue except front end (Until and unless you use another auxiliary data structure). If you want a similar functionality then even before an std:: list go blindly for an std:: vector. Profile your code and if you feel the need of std:: list go for it.

Related