Phong
Phong

Reputation: 6768

How is the memory use in a queue?

In my project I use the std::queue class. I would like to know what happen if I do the following.

Does my pointer still point on the same element I specify in the beginning ? Is it defined by the queue specification?

Upvotes: 0

Views: 1283

Answers (2)

James McNellis
James McNellis

Reputation: 355069

std::queue uses a sequence container for its implementation. By default, std::deque is used. With a std::deque, so long as all the insertions and erasures are at the beginning or the end of the container, references and pointers to elements in the container are not invalidated.

However, I don't know how you are going to get a pointer to an element in the queue; it doesn't provide functionality for that (you can only get a reference to the first and last elements in the queue).

Upvotes: 1

Michael Anderson
Michael Anderson

Reputation: 73490

Just use a std::deque instead. std::queue is designed to prevent the user from doing anything non-queue like.

Upvotes: 1

Related Questions