C ++ Primer study note _ 55_STL analysis (10): Container adapter (stack, queue, priority_queue) source code analysis and use example

Source: Internet
Author: User

C ++ Primer study note _ 55_STL analysis (10): Container adapter (stack, queue, priority_queue) source code analysis and use example

Seven basic containers: vector, deque, list, set, multiset, map, and multimap

I. Container Adapter
Stack
Queue
Priority_queue

Stack, queue, and priority_queue do not support any type of iterator. They are all container adapter types. stack uses the vector/deque/list object to create an advanced post-output container; queue creates a first-in-first-out container using the deque or list object. priority_queue creates a sort queue using the vector/deque and implements it using the binary heap internally.

Ii. stack

1. Example

# Include <iostream> # include <vector> # include <list> # include <stack> using namespace std; int main (void) {stack <int, int = ""> s; // set indicates that an error occurs for (int I = 0; I <5; I ++) {s. push (I);} // for (size_t I = 0; I <s. size (); cout = "" pre = "" return = "" while = ""> <p> </p> <div> running result: </div> <div> 4 3 2 1 0 </div> <p> 2. source code analysis </p> </p> <pre class = "brush: java; "> // template class stacktemplate <class _ Ty, class _ Contai Ner = deque <_ Ty> class stack {// LIFO queue implemented with a containerpublic: typedef _ Container container_type; typedef typename _ Container: value_type; typedef typename _ Container :: size_type; typedef typename _ Container: reference; typedef typename _ Container: const_reference; stack (): c () {// construct with empty container} explicit stack (con St _ Container & _ Cont): c (_ Cont) {// construct by copying specified container} bool empty () const {// test if stack is empty return (c. empty ();} size_type size () const {// test length of stack return (c. size ();} reference top () {// return last element of mutable stack return (c. back ();} const_reference top () const {// return last element of nonmutable stack return (c. back ();} void pu Sh (const value_type & _ Val) {// insert element at end c. push_back (_ Val);} void pop () {// erase last element c. pop_back ();} const _ Container & _ Get_container () const {// get reference to container return (c);} protected: _ Container c; // the underlying container }; </pre> <p> </p> <div> There Is A _ Container member. The default value is deque <_ Ty>. Of course, vector and list can also be passed in, you only need to support interfaces such as push_back and pop_back. Internal function implementations all use container functions. </Div> <p> </p> <p> 3. queue </p> <p> 1, example </p> <pre class = "brush: java; ">#include <iostream> # include <vector> # include <list> # include <stack> # include <queue> using namespace std; int main (void) {// int a [] = {1, 2, 3, 4, 5}; // vector <int> v (a, a + 5 ); // queue <int> q (a, a + 5) // error. The queue cannot be initialized like this <int, int = ""> q; // a vector error occurs, you must support the pop function for (int I = 0; I <5; I ++) {q. push (I) ;}while (! Q. empty () {cout <q. front () <''; q. pop () ;}cout <endl; return 0 ;}</int,> </int> </queue> </stack> </list> </vector> </iostream> </pre> <p> </p> <div> running result: </div> <div> 0 1 2 3 4 </div> <p> 2. source code analysis </p> <p> </p> <pre class = "brush: java; "> // template class queuetemplate <class _ Ty, class _ Container = deque <_ Ty> class queue {// FIFO queue implemented with a containerpublic: typedef _ C Ontainer identifier; typedef typename _ Container: value_type; typedef typename _ Container: size_type identifier; typedef typename _ Container: reference; typedef typename _ Container: const_reference; queue (): c () {// construct with empty container} explicit queue (const _ Container & _ Cont): c (_ Cont) {// construct by copying specified container} bool empty () Const {// test if queue is empty return (c. empty ();} size_type size () const {// return length of queue return (c. size ();} reference front () {// return first element of mutable queue return (c. front ();} const_reference front () const {// return first element of nonmutable queue return (c. front ();} reference back () {// return last element of mutable queue return (c. back ();} const_re Ference back () const {// return last element of nonmutable queue return (c. back ();} void push (const value_type & _ Val) {// insert element at beginning c. push_back (_ Val);} void pop () {// erase element at end c. pop_front ();} const _ Container & _ Get_container () const {// get reference to container return (c);} protected: _ Container c; // the underlying container }; </pre> <p> </p> <div> implementation is similar to stack implementation. Only queue cannot be implemented using vector, because there is no pop_front interface. </Div> <p> </p> <p> 4. priority_queue </p> <p> 1, example </p> <pre class = "brush: java; ">#include <iostream> # include <functional> # include <vector> # include <list> # include <stack> # include <queue> using namespace std; int main (void) {int a [] = {5, 1, 2, 4, 3}; priority_queue <int, int = "">, greater <int> q (a, a + 5); // The value ranges from large to small by default. You must have three parameters to call greater <int> // priority_queue <int, int = "">, less <int> q (a, a + 5); while (! Q. empty () {cout <q. top () <''; q. pop () ;}cout <endl; return 0 ;}</int> </int,> </int,> </queue> </stack> </list> </vector> </functional> </iostream> </pre> <p> </p> <div> running result: </div> <div> 1 2 3 4 5 </div> <p> 2. source code analysis </p> <p> </p> <pre class = "brush: java; "> // template class priority_queuetemplate <class _ Ty, class _ Container = vector <_ Ty>, class _ Pr = less <typename _ container: v Alue_type = ""> class names {// priority queue implemented with a _ Containerpublic: typedef _ Container container_type; typedef typename _ Container: value_type; typedef typename _ Container :: size_type; typedef typename _ Container: reference; typedef typename _ Container: const_reference; priority_queue (): c (), comp () {// construct with empty co Ntainer, default comparator} explicit priority_queue (const _ Pr & _ Pred): c (), comp (_ Pred) {// construct with empty container, specified comparator} priority_queue (const _ Pr & _ Pred, const _ Container & _ Cont): c (_ Cont), comp (_ Pred) {// construct by copyspecified container, comparator make_heap (c. begin (), c. end (), comp);} template <class_iter> priority_queue (_ Iter _ First, _ Iter _ Last): c (_ Fir St, _ Last), comp () {// construct by copying [_ First, _ Last), default comparator make_heap (c. begin (), c. end (), comp);} template <class_iter> priority_queue (_ Iter _ First, _ Iter _ Last, const _ Pr & _ Pred): c (_ First, _ Last), comp (_ Pred) {// construct by copying [_ First, _ Last), specified comparator make_heap (c. begin (), c. end (), comp);} template <class_iter> priority_queue (_ Iter _ First, _ Iter _ Last, c Onst _ Pr & _ Pred, const _ Container & _ Cont): c (_ Cont), comp (_ Pred) {// construct by copying [_ First, _ Last ), container, and comparator c. insert (c. end (), _ First, _ Last); make_heap (c. begin (), c. end (), comp);} bool empty () const {// test if queue is empty return (c. empty ();} size_type size () const {// return length of queue return (c. size ();} const_reference top () const {// return highest-pri Ority element return (c. front ();} reference top () {// return mutable highest-priority element (retained) return (c. front ();} void push (const value_type & _ Pred) {// insert value in priority order c. push_back (_ Pred); push_heap (c. begin (), c. end (), comp);} void pop () {// erase highest-priority element pop_heap (c. begin (), c. end (), comp); c. pop_back ();} protected: _ Container c; // the underlying Container _ Pr comp; // the comparator functor }; </class_iter> </typename> </pre> <p> </p> <p> the implementation of priority_queue is slightly more complex, three parameters can be passed, and there are two members. The comp is the custom comparison logic. The default value is less <value_type>. In the constructor, The make_heap function is called to construct a binary heap, comp is mainly used to identify the construction of a binary heap. If less is used, a large heap is constructed. If greater is passed, a small heap is constructed. </value_type> </p> <p> Note: priority_queue cannot be implemented using list, because list only supports bidirectional iterators, but does not support random iterators. </P> <p> The following example describes how to use the make_heap function (to construct a binary heap ): </p> <pre class = "brush: java; "> # include <iostream> # include <functional> # include <vector> # include <list> # include <stack> # include <queue> # include <iterator> using namespace std; int main (void) {int a [] = {5, 1, 2, 4, 3}; make_heap (a, a + 5, less <int> ()); copy (a, a + 5, ostream_iterator <int> (cout, ""); cout <endl; sort (a, a + 5); // sort_heap (A, a + 5, less <int> (); // less copy (a, a + 5, ostream_iterator <int> (cout, ""); cout <endl; return 0 ;} </int> </iterator> </queue> </stack> </list> </vector> </functional> </iostream> </pre> <p> </p> <div> output: </div> <p> 5 4 2 1 3 </p> <p> 1 2 3 4 5 </p> <p> make_heap () construct the elements of the container into a binary heap and transmit less, that is, construct a large heap, store the result of the large heap sequence traversal into an array, and then call sort () for sorting, the actual algorithms called internally are not necessarily heap sorting, insert sorting, and select sorting. When tracing, we can find that insertion sorting is called. Of course, we can also directly specify Heap sorting sort_heap (the caller must already be a heap, that is, make_heap has been called before, and the size heap type must match), just like make_heap, the third parameter passes the usage of the function object. By default, sort and sort_heap are sorted from small to large. Unless the overloaded version passes the third parameter, the third parameter can be a function pointer or a function object: </p> <pre class = "brush: java;"> // order heap by repeatedly popping, using operator <template <class _ ranit = ""> inlinevoid sort_heap (_ RanIt _ First, _ RanIt _ Last); // order heap by repeatedly popping, using _ Predtemplate <class _ RanIt, class _ Pr> inlinevoid sort_heap (_ RanIt _ First, _ RanIt _ Last, _ Pr _ Pred ); </class> </pre> <p> </p> <div> transmits greater to construct a small heap, as shown in: </div> <p>  </p> <p> </p> <p> reference: </p> <p> C ++ primer version 4 Objective C ++ 3rdC ++ programming specifications </p> <div> </s. size ();> </int,> </stack> </list> </vector> </iostream>

Related Article

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.