C ++ Implementation of sorted_queue

Source: Internet
Author: User

Sorted_queue is actually called sorted_list. Based on various considerations, I set the underlying container of sorted_queue to deque, which seems to compromise the vector and list.

The insertion of sorted_queue is ordered, so there is an overhead for moving elements. The deletion operation also moves the elements. However, sorted_queue supports random access, provides a random iterator, and can be used for algorithms of the ordered interval. The definition of sorted_queue can also be improved, such as adding constructors with parameters and swap member methods.

The definition and implementation of sorted_queue are as follows:

 

/*************************************** * **************************** Created: created: file base: sorted_queue file ext: h author: Justme0 (http://blog.csdn.net/Justme0) purpose: definition and implementation of sorted_queue *********************************** * ********************************/# ifndef SORTED_QUEUE_H # define SORTED_QUEUE_H # include <deque> # include <algorithm> template <class T> Class sorted_queue {public: typedef typename std: deque <t >:: value_type; typedef typename std: deque <t >:: size_type; typedef typename std :: deque <t >:: reference; typedef typename std: deque <t >:: const_reference; typedef typename std: deque <t >:: iterator; typedef typename std: deque <t >:: const_iterator; protected: std: deque <T> c ;// Underlying container public: iterator begin () {return c. begin ();} const_iterator begin () const {return c. begin ();} iterator end () {return c. end ();} const_iterator end () const {return c. end ();}/**** direct access */reference operator [] (size_type n) {return c [n];} const_reference operator [] (size_type n) const {return c [n];} bool empty () const {return c. empty ();} size_type size () const {return c. size ();} re Ference front () {return c. front ();} const_reference front () const {return c. front ();} reference back () {return c. back ();} const_reference back () const {return c. back ();}/*** insert element */void push (const value_type & x) {c. insert (lower_bound (c. begin (), c. end (), x), x);} void pop_front () {c. pop_front ();} void pop_back () {c. pop_back ();} iterator erase (const_iterator position) {return c. er Ase (position);} iterator erase (const_iterator first, const_iterator last) {return c. erase (first, last);}/*** delete all elements with x values */void remove (const value_type & x) {/*** the following statement must use auto, because it does not know whether iterator or const_iterator is returned. ** It is more efficient to use equal_range. */Auto interval = pai_range (this-> begin (), this-> end (), x); this-> erase (interval. first, interval. second);} void clear () {c. clear () ;};# endif
/*************************************** * **************************** Created: created: file base: test file ext: cpp author: Justme0 (http://blog.csdn.net/Justme0) purpose: test Program ************************************ ********************************/# include <iostream> # include "sorted_queue.h" using namespace std; template <class T> void output (const sorted _ Queue <T> & sq) {for (auto ite = sq. begin (); ite! = Sq. end (); ++ ite) {cout <* ite <"" ;}cout <endl ;}int main (int argc, char ** argv) {sorted_queue <int> sq; sq. push (1); sq. push (-1); sq. push (0); sq. push (0); output (sq); cout <"[3] =" <sq [3] <endl; cout <"front =" <sq. front () <endl; cout <"back =" <sq. back () <endl; cout <(sq. empty ()? "Empty": "not empty") <endl; cout <"erase 0" <endl; sq. remove (0); output (sq); cout <endl; cout <"clear" <endl; sq. clear (); cout <(sq. empty ()? "Empty": "not empty") <endl; return 0 ;}
-1 0 0 1 [3] = 1 front =-1 back = 1 not empty erase 0-1 1 clear empty press any key to continue... -1 0 0 1 [3] = 1 front =-1 back = 1not emptyerase 0-1 1clearempty press any key to continue...

 

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.