Exploring STL container Adapter

Source: Internet
Author: User
Container Adapter

 

Features

 

Use a certain sequence of containers (to make existing sequence containers work in the form of stacks/queues)

 

Category

 

1) STACK: header file <stack>
? Stack -- post-import, first-out

2) queue: header file <queue>
? Queue-FIFO

3) priority_queue: header file <queue>
? Priority queue -- the highest priority element is always the first column

 

Note:


There is no iterator on the container Adapter
The sorting, searching, and variable order algorithms in STL are not suitable for container adapters.

 

 

 

 

Stack

 

Features

 

1. Stack is the data structure of "back-to-first-out"

2. You can only insert, delete, and access the elements at the top of the stack.

3. It can be implemented using vector, list, And deque.

? By default, deque is used

? Implement with vector and deque, better performance than implement with List

 

Template <class T, class cont = deque <t> // The first is the stack data type, and the second is the implemented container. The default value is deque class Stack {...};


 

Member Functions

 

Constructor

 

Stack <int> stde1; // stack <int, deque <int> stde2; stack <int, vector <int> stvc; stack <int, list <int> stli;


 

Access element operations and types

Size_type

Number of container elements (unsigned integer)

Value_type

Element type in Container

Empty ()

Determines whether the stack is empty. If yes, true is returned.

Pop ()

Remove top stack Elements

Push ()

Add elements to the top of the stack

Size ()

Returns the number of stack elements.

Top ()

Returns the reference of the top element of the stack.

 

 

Example

 

# Include "stdafx. H "# include <vector> # include <list> # include <deque> # include <stack> # include <iostream> using namespace STD; int main () {stack <int> dest1; stack <int, deque <int> dest2; stack <int, vector <int> vest; stack <int, list <int> list; stack <int >:: size_type m; stack <int >:: value_type N; dest1.push (5); dest1.push (50); dest1.push (500 ); cout <"the number of dest1 elements is:"; M = dest1.size (); cout <m; cout <"\ n "; // 3 cout <"the top element of dest1 is:"; n = dest1.top (); cout <n; cout <"\ n"; // 500dest1. pop (); cout <"after execution of pop, the top element of dest1 stack is:"; cout <dest1.top (); // 50 getchar (); Return 0 ;}


 

 

 

 

 

 

Queue

 

Features

 

1. You can use list and deque. By default, deque is used. It stores container objects for it to implement all functions.

2. It only allows deletion at the front end of the table, and insertion at the back end of the table.

3. The first element to be inserted is deleted first, and the last element to be inserted is deleted.

 

 

template<class T, class Cont = deque<T> > class queue {    ……};

 

 

Member Functions

 

Constructor

 

   queue <int> quede1;   queue <int, deque<int> > quede2;   queue <int, list<int> > queli;


 

Access the elements in the queue

 

Size_type

Number of container elements (unsigned integer)

Value_type

Element type in Container

Empty ()

Determine whether the queue is empty

Size ()

Returns the number of queue elements.

Pop ()

Remove top queue Elements

Push ()

Add elements to the end of the queue

Front ()

Returns the reference of the top element of the queue.

Back ()

Returns the last element of the recently inserted queue.

 

 

 

Example:

 

 

# Include "stdafx. H "# include <list> # include <deque> # include <queue> # include <iostream> using namespace STD; int main () {queue <int> que1; queue <int, deque <int> que2; queue <int, list <int> que3; que1.push (5); que1.push (9); que1.push (20 ); que1.push (200); cout <"the number of elements in queue que1 is:"; cout <que1.size (); // 4 cout <"\ n "; cout <"the front element of the queue is:"; cout <que1.front (); // 5 cout <"\ n"; cout <"The back element of the queue is: "; cout <que1.back (); // 200 cout <" \ n "; que1.pop (); cout <" pop after execution. The number of elements in queue que1 is: "; cout <que1.size (); // 3 cout <" \ n "; cout <" pop after execution, the front element of the queue is :"; cout <que1.front (); // 9 cout <"\ n"; cout <"pop after execution, the back element of the queue is :"; cout <que1.back (); // 200 cout <"\ n"; getchar (); Return 0 ;}


 

 

 

 

Priority_queue

 

Features:

 

Priority_dueue also limits the access to the controlled sequence, but it still has some additional requirements. It is actually a queue, but this queue uses

ItemsPredicateTo check which element has the highest priority. This template class ensures that each time top is passed, all elements obtained from it are left.

The one with the highest priority in the element. To do this, every time an element is added to it through push, the entire sequence it controls will be necessary

. More specifically, it maintains the controlled sequence as a heap and uses some algorithms.

 

1. You can implement it using vector and deque. By default, it is implemented using vector.

2. priority_queue is usually implemented by heap sorting technology to ensure that the largest element is always at the beginning

? During the pop operation, the largest element is deleted.

? When the top operation is executed, the reference of the largest element is returned.

3. The default element comparator is less <t>

 

 

Member Functions

 

Constructor

 

 

Priority_queue <int> q1; priority_queue <int, deque <int> Q2; priority_queue <int, vector <int>, greater <int> Q3; // great <int> is the preceding preority_queue <int> Q4 (Q1); priority_queue <int, vector <int>, greater <int> Q6 (v5.begin (), v5.begin () + 2 );


 

Access elements in priority queue

 

Size_type

Number of container elements (unsigned integer)

Value_type

Element type in Container

Empty ()

Determines whether the priority queue is null. If yes, true is returned.

Pop ()

Remove the largest element from the top of the priority queue

Push ()

Add elements to the priority queue

Size ()

Returns the number of priority queue elements.

Top ()

Returns the constant reference of the largest element at the top of the priority queue.

 

 

Example:

 

 

# Include "stdafx. H "# include <vector> # include <list> # include <deque> # include <queue> # include <iostream> using namespace STD; int main () {priority_queue <int> q1; q1.push (5); q1.push (50); q1.push (2); q1.push (110); cout <"number of elements in priority queue Q1: "; cout <q1.size (); cout <" \ n "; // 4 cout <" the top element of the priority queue is: "; cout <q1.top (); cout <"\ n"; // 110q1. pop (); cout <"After Pop is executed, the top element of the priority queue is:"; cout <q1.top (); // 50 getchar (); Return 0 ;}


 

 

 

 

 

 

 


 

 

 

 

 

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.