1) Overview
Deque is a sequence container and a bidirectional open continuous linear space. You can insert and delete elements at the beginning and end respectively. Of course, you can also perform a vector action at both ends of the header and end,
C + + in the STL is still more useful, especially in the implementation of scientific research algorithms, but also useful, but not how the system has been studied, so recently looked for a book, Ye Zhijun the "C + + STL development Technology
The collections module in Python provides a number of useful collection classes, where the deque can be described as a list of enhanced editions.
Deque basically have all the list methods:
From collections Import deque
q=deque ([1,2,3])
q.append (1)
To create a deque sequence:
From collections import deque
d = deque ()
Deque provides a list-like approach to operations:
D = deque ()
d.append (' 1 ')
d.append (' 2 ')
d.append (' 3 ')
len (d)
d[0]
d[-1]
The Deque module is an item in the Python standard library collections, which provides a sequence that can be manipulated at both ends, which means that you can perform add or delete operations before and after the sequence.1. Create the deque
In the writing of C + + program will find that STL is a good thing, reduce the amount of code, so that the code reuse rate greatly improved, reduce the burden of the program ape. There is a container, you will find that if you write a list, queue,
Create a Deque sequence:
From collections import dequed = deque ()
Deque provides a list-like operation method:
D = deque () d. append ('1') d. append ('2') d. append ('3') len (d) d [0] d [-1]
Output result:
3 '1' 3'
Both ENDS Use pop:
D =
Back to General bookIn the original section: http://www.cplusplus.com/reference/deque/deque/1. std::d equeTemplate > class deque;Dual-ended queuesDeque (pronounced like "deck") is a special abbreviation for Double-ended-queUE. Deque is a
Deque of C ++ STL containers
Compared with the vector container, deque dual-end queue container has the obvious advantage of efficiently adding and deleting the first and last elements, and is easy to insert elements at any position.Adding an
Vector is the earliest STL container I used, and it is easy to use. Its structure is clear and easy to understand. deque is somewhat mysterious. One of the most notable features of deque is that it can insert members from the front-end of the
Create a two-way queueImport= Collections.deque ()Append (add an element to the right)Import= collections.deque () d.append (1) d.append (2)print(d) # output: deque ([1, 2])Appendleft (add an element to the left)Import= collections.deque ()
First, when considering the memory allocation and execution performance, it is better to use std: deque than std: vector.
Deque Overview
Deque and vector are both in the standard template library. deque is a dual-end queue, which is very similar to
/****************************************** ***** Deque:**************************************** ******* Random access is allowed. elements are inserted and deleted in the ** header and tail **. the time complexity is O (1)* Random Access container
VectorSimilar to the built-in array,It has a continuous memory and the starting address remains unchanged.Therefore, it supports random access, that is, the [] Operator. However, because its memory space is continuous, inserting and deleting in the
Create a two-way queueImport COLLECTIONSD = Collections.deque ()Append (add an element to the right)Import COLLECTIONSD = Collections.deque () d.append (1) d.append (2) print (d) #输出: Deque ([1, 2])Appendleft (add an element to the left)Import
Create a two-way queueImport COLLECTIONSD = Collections.deque ()Append (add an element to the right)Import COLLECTIONSD = Collections.deque () d.append (1) d.append (2) print (d) #输出: Deque ([1, 2])Appendleft (add an element to the left)Import
Create a two-way queue
Import COLLECTIONSD = Collections.deque ()
Append (add an element to the right)
Import COLLECTIONSD = Collections.deque () d.append (1) d.append (2) print (d) #输出: Deque ([1, 2])
Appendleft (add an element to the left)
Import
In STL, the basic containers include string, vector, list, deque, set, and map.
Set and map are unordered storage elements. They can only be accessed through the interfaces provided by it;
Set: set, which is used to determine whether an element is
Deque is an abbreviation for double-ended queue, similar to list, but provides an action to insert and delete at both ends.
Appendleft inserts to the left of the list
Popleft the value on the left side of the pop-up list
Extendleft
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.