STL learning, stl

Source: Internet
Author: User

STL learning, stl

Queue is the Queue representation in STL.

  • The feature is First In First Output FIFO ).
  • The operation is to add and remove elements, add elements from the bottom, and obtain elements from the top, except for adding elements from the bottom and retrieving elements from the top, there is no other way to access other elements of the queue, that is, the queue does not allow traversal.

  • Push the element to the queue and push the element to the pop.

  • Because the queue system completes all its work with the bottom container, and has this "Modify something interface, forming another style" Nature, called adapter (container), therefore, STL queue is often called the container adapter instead of being classified as a container.

  • The underlying container functions of queue include empty, size, front, back, push_front, push_back, pop_front, and pop_back. Therefore, if the list is used as the bottom container and some interfaces are closed, a queue can be easily traveled.

The following is some of the source code of queue. You can see some implementations and method definitions of queue.

When I look at Hou Jie's STL source code parsing, it says that the queue method is actually simple.listYou can easily implement the queue. UsagelistFor the implementation code, see STL source code analysis.

/*    queue synopsisnamespace std{template <class T, class Container = deque<T>>class queue{public:    typedef Container                                container_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<typename Container::value_type>>class priority_queue{public:    typedef Container                                container_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;    Compare comp;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,            priority_queue<T, Container, Compare>& y)            noexcept(noexcept(x.swap(y)));}  // std*/

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.