The most intuitive way would be to use std::vector as the underling container of your std::queue to provide continuous memory, and then take the address std::queue::front to access the continuous storage of the wrapped std::vector .
However, since @Blastfurnace correctly noted that std::vector does not have a member function pop_front , and when you are going to call std::queue::pop , the program will burst.
Also, as already mentioned, std::queue usually required as the main container, a container of type std::deque or std::list . Due to their structural features, these containers are the most suitable, because they can effectively use their front element. However, due to its contiguous memory, std::vector lacks such versatility.
Also, if using a container with contiguous memory is something that would greatly enhance your application, it is better and easier to use std::vector .
However, for recording, you can also do the following hack to get around the lack of the member function std::vector pop_front . You can define a class that inherits from std::vector and implement the member function pop_front , as shown below:
#include <vector> #include <queue> #include <iostream> #include <algorithm> template<class T, class Alloc = std::allocator<T>> class queuevec : public std::vector<T, Alloc> { public: void pop_front() { if(!this->empty()) this->erase(this->begin()); } }; int main() { std::queue<int, queuevec<int>> Q; for(int i(0); i < 10; ++i) Q.push(i); Q.pop(); int *t = &(Q.front()); std::for_each(t, t + Q.size(), [](int const i){ std::cout << i << " "; }); std::cout << std::endl; }
Live demo
101010
source share