implement queue using vector
Apr 22, 2016 at 8:08pm Apr 22, 2016 at 8:08pm UTC
How can I implement queue using vector.
Also What does this mean
Apr 22, 2016 at 8:58pm Apr 22, 2016 at 8:58pm UTC
push : push to the back of the queue (vector -
push_back )
pop : pop from the front of the queue (vector - simulate
pop_front by erasing the item at the front)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <vector>
#include <queue>
#include <iostream>
#include <iomanip>
template < typename T > struct my_vec : std::vector<T> // brute force implementation
{
using base = std::vector<T> ;
using base::base ;
using typename base::value_type ;
using base::empty ;
using base::size ;
using base::front ;
using base::back ;
using base::push_back ;
void pop_front() { base::erase( base::begin() ) ; }
};
int main()
{
std::queue< int , my_vec<int > > my_queue ;
for ( int v : { 12, 78, 56, 22, 99, 34, 56 } ) my_queue.push(v) ;
while ( !my_queue.empty() )
{
std::cout << my_queue.front() << ' ' ;
my_queue.pop() ;
}
std::cout << '\n' ;
}
http://coliru.stacked-crooked.com/a/654736a869a8c490
http://rextester.com/FSFVTI19806
Topic archived. No new replies allowed.