STL Learning-Queue Understanding

Source: Internet
Author: User

Queue is the representation of queues in the STL.

    • The feature is FIFO (first in the Output FIFO).
    • The allowed actions are new elements, remove elements, add elements from the bottom, get elements from the top, and, in addition to adding elements from the bottom, taking out from the top, there is no other way to access other elements of the queue, which means that the queue does not allow traversal behavior.

    • Pushing elements into the queue is push, and the action of putting elements out of queue is pop.

    • Because the queue system does all its work with the bottom container, and has the property of "modifying something interface to form another style", called Adapter (container), the STL queue is often not classified as container (container), and is called container Adapter.

    • The functions of the bottom container of the queue are empty, size, front, back, Push_front, Push_back, Pop_front, Pop_back. So if you use list as the bottom container, close some interfaces, you can easily travel a queue.

Here are some of the queue's source code, you can see from the inside of the queue some of the implementation and method definition.

I am looking at Houtie STL source parsing, the inside said that in fact, the method of the queue is simple, list some of the interface to the closure, you can easily implement the queue. Specific use list of the implementation of the code, you can see the "STL Source Analysis" this book.

/* Queue Synopsisnamespacestd{Template<class T,class Container= deque<T>>classQueue{public:typedefContainerContainer_type;    typedef typename CONTAINER_TYPE::VALUE_TYPE Value_type;    typedef typename Container_type::reference Reference;    typedef typename Container_type::const_reference Const_reference; typedef typename Container_type::size_type Size_type;protected:container_type C;public:queue()= default; ~queue()= default; Queue(const queue& q)= default; Queue(queue&& q)= default; queue& operator=(const queue& q)= default; queue& operator=(queue&& q)= default; Explicit queue(const container_type& c); Explicit queue(container_type&& c)Template <class Alloc> Explicit queue(const Alloc& a); Template <class Alloc> Queue(const container_type& C, const Alloc& A ); Template <class Alloc> Queue(container_type&& c, const Alloc& a); Template <class Alloc> Queue(const queue& q, const Alloc& a) ; Template <class Alloc> Queue(queue&& q, const Alloc& a); BOOL Empty()Const Size_type size()Const Reference Front(); Const_reference Front()Const Reference back(); Const_reference back()Const void push(const value_type& v); void push(value_type&& v); Template <class...Args> void Emplace(Args&& ....) args); void pop(); void swap(queue& q)Noexcept(noexcept(swap(c, Q).  C)));}; Template <class T,class Container> BOOL operator==(const queue<T, Container>& x,const  Queue<T, Container>& y); template <class T,class Container> BOOL operator<(const queue<T, Container>& x,const queue <T, Container>& y); template <class T,class Container> BOOL operator!=(const queue<T, Container>& x,const queue <T, Container>& y); template <class T,class Container> BOOL Operator>(const queue<T, Container>& x,const queue <T, Container>& y); template <class T,class Container> BOOL operator>=(const queue<T, Container>& x,const queue <T, Container>& y); template <class T,class Container> BOOL operator<=(const queue<T, Container>& x,const queue <T, Container>& y); template <class T,class Container> void Swap(queue<t, Container>& x, queue<T,  Container>& y)Noexcept(noexcept(x.  Swap(y))); template <class T,class Container= vector<T,class Compare= Less<typenameContainer::value_type>>classPriority_queue{public:typedefContainerContainer_type;    typedef typename CONTAINER_TYPE::VALUE_TYPE Value_type;    typedef typename Container_type::reference Reference;    typedef typename Container_type::const_reference Const_reference; typedef typename Container_type::size_type Size_type;protected:container_type C;CompareComp;public:priority_queue()= default; ~priority_queue()= default; Priority_queue(const priority_queue& q)= default; Priority_queue(priority_queue&& q)= default; priority_queue& operator=(const priority_queue& q)= default; priority_queue& operator=(priority_queue&& q)= default; Explicit priority_queue(const Compare& comp); Priority_queue(const Compare& Comp, const container_type& C) ; Explicit priority_queue(const Compare& Comp, container_type&& c) ; Template <class Inputiterator> Priority_queue(inputiterator First, inputiterator last, const 
      
       compare& 
       Comp = 
       Compare()
      ); Template <class Inputiterator> Priority_queue(inputiterator First, inputiterator last, const 
      
       compare& 
       Comp, 
       const 
       container_type& 
       c)
      ; Template <class Inputiterator> Priority_queue(inputiterator First, inputiterator last, const 
      
       compare& 
       Comp, 
       container_type&& 
       c)
      ; Template <class Alloc> Explicit priority_queue(const Alloc& a); Template <class Alloc> Priority_queue(const Compare& Comp, const Alloc& a); Template <class Alloc> Priority_queue(const Compare& Comp, const container_type& c, const Alloc& a); Template <class Alloc> Priority_queue(const Compare& Comp, container_type&& C, Const Alloc& a); Template <class Alloc> Priority_queue(const priority_queue& q, const Alloc& A ); Template <class Alloc> Priority_queue(priority_queue&& q, const Alloc& a); BOOL Empty()Const Size_type size()Const Const_reference Top()Const void push(const value_type& v); void push(value_type&& v); Template <class...Args> void Emplace(Args&& ....) args); void pop(); void swap(priority_queue& q)Noexcept(noexcept(swap(c, Q).  C)) && noexcept(swap(comp.  Q. Comp)));}; Template <class T,class Container,class Compare> void Swap(priority_queue<T, Container, Compare>& x, prior Ity_queue<T, Container, Compare>& y)Noexcept(noexcept(x.  Swap(y)));} std*/

STL Learning-Queue Understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.