Joxixi
Joxixi

Reputation: 721

Efficieny of stl vector and queue

I want to implement a program using either vector or queue data structure. The function requires frequently pushing and popping some elements (e.g. 8 elements) from the data structure to process these elements, and finally the data structure will be empty. The processing order doesn't matter so both vector and queue are ok. I want to know which one has higher efficiency if I need to frequently push and pop from it. Thank you.

Upvotes: 0

Views: 215

Answers (1)

eerorika
eerorika

Reputation: 238321

std::queue isn't a container. It is a container adapter. By default, it adapts std::deque.

The function requires frequently pushing and popping some elements ... The processing order doesn't matter

This seems to imply that you can pop from the same end where you push (LIFO). In that case, another container adaptor would be appropriate: std::stack. It also adapts std::deque by default. There is no difference in efficiency of std::vector and the efficiency of std::stack that adapts std::vector. There can be a difference in using std::deque depending on the details of how you use it.

Upvotes: 2

Related Questions