Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.data() equivalent for std::queue

My question is simple: Its possible to obtain a pointer to the underlying storage of a std::queue container adapter?

I'm working on some simulations using SFML for rendering, and I use the draw() method of the SFML render target (sf::RenderTarget) to draw the entire bunch of data. That method has a C-like interface expecting a pointer to the data and a std::size_t with the number of elements to draw.

Since the data is stored in a queue for some purposes, I will be glad if there is some way to get that pointer to the queue underlying storage instead of copying the data to a vector.
I know that std::queue adapts the container std::deque by default, but I don't know how that circular buffer is implemented and if its data is contiguous (So I can extract a pointer to the data directly).

EDIT: Performance

Looking at the answers bellow, let me note that I'm not using std::deque because its fancy queue-like interface, but because I really need fast queueing. Of course I can use std::vector. If performance wasn't the point here I would have used push_back()and erase( begin() ) of vector. But what I need is fast queueing and a way to move the data of that queue to the render target efficiently. Of course if the balance queueing vs effort to draw it goes to the draw side, I will use std::vector.

like image 788
Manu343726 Avatar asked Jul 05 '14 19:07

Manu343726


1 Answers

Is it possible to obtain a pointer to the underlying storage of a std::queue container adapter?

Short answer: No.

std::queue requires a SequenceContainer type, like std::deque or std::list. Neither of these guarantee contiguous storage, so there's no concept of a pointer to the underlying storage.

If they used contiguous circular buffers, this would either impose a fixed size on the container, or incur large cost (like std::vector can) when the container needs to be resized.

You could look at using a boost::circular_buffer instead.

like image 143
Roddy Avatar answered Sep 26 '22 02:09

Roddy