Home > Mobile >  Getting a specific element from Queue
Getting a specific element from Queue

Time:06-28

I have a queue: queue<string> saves. I need to use it's ability to remove elements from the front. Is there a way to get a specific element in the queue like the 5th one?
Neither saves.at(4) nor saves[4] works.

CodePudding user response:

The entire point of a queue is you can only see and get the item, if any, waiting at the out end. Anything that allows you to see any other values in the queue is not a queue.

Wanting to see inside the queue suggests that you do not want a queue. Try a std::deque instead.

CodePudding user response:

If you can try another data structure, try std::list,

But in this case, List Does not have 'at' method

So if you have to access element with index, see also How to get an element at specified index from c List

  •  Tags:  
  • c
  • Related