[C ++ STL 3] Deep Learning of container deque

Source: Internet
Author: User

The C ++ STL container deque is similar to the vector and uses dynamic arrays to manage elements.

Before using deque, you must include the header file:

# Include <deque>

It is a class template defined in the namespace STD:

Template <class _ ty,
Class _ AX = Allocator <_ ty>
Class deque;

The first template parameter is used to indicate the element type. The second parameter is optional and specifies the memory model. The default memory model is generally used.

Unlike vector, deque's dynamic array is open at the beginning and end, so it can be quickly inserted and deleted at the beginning and end.

The logical structure of deque:



Deque's internal structure

Deque is an optimized basic sequence container for adding and deleting elements at both ends of a sequence. It is usually composed of several independent blocks. The first block is extended in a certain direction, and the last block is extended in another direction. It allows fast random access, but it does not store all objects in a continuous memory block like a vector, but multiple consecutive memory blocks. In addition, the tracing of these blocks and their sequence is saved in a ing structure.

Shows the internal structure:


Deque features:

1. Random Access is supported, that is, [] and at () are supported, but the performance is not good at vector.

2. You can insert or delete data internally, but the performance is not as good as list.


Difference between deque and vector:

1. Both ends can be used to quickly insert and delete elements. The vector can only be performed at the end.

2. Element access and iterator operations of deque will be slightly slower. Because deque's internal structure has an indirect process.

3. The iterator is a special smart pointer instead of a general pointer. It needs to jump between different blocks.

4. deque can contain more elements, and its max_size may be larger. Because more than one memory is used.

5. Capacity and memory allocation timing cannot be controlled.

Note: inserting or deleting an element at the beginning and end of the element will invalidate any pointers, references, and iterators pointing to the deque element. However, deque's memory reallocation is better than that of vector. Because its internal structure shows that no elements need to be copied.

6. When deque's memory block is no longer used, it will be released. The memory size of deque can be reduced. However, whether or not this is done and how it is defined by the actual version.

 

Similar features of deque and vector:

1. It is relatively slow to insert or delete elements in the middle part because all elements must be moved.

2. The iterator is an access iterator.

 

Deque is recommended:

1. You need to insert and delete elements at both ends.

2. You do not need to reference elements in the container.

3. The container is required to release elements that are no longer in use.

 

Operation functions of deque

Constructor and destructor:

Non-variable operations:


Variable operation:



The operations of deque are only two different points from those of vector:

Deque does not provide capacity operations: Capacity () and reverse ().

Deque directly provides a function to insert and delete the first and last elements.

The rest are the same as the vector.

Note:

1. Except for the at () function, other member functions do not check whether the index or iterator is valid.

2. Element insertion and deletion may cause memory reallocation. Therefore, any insert or delete operation will invalidate all pointers, reference, and iterators pointing to the deque element. The only exception is that after an element is inserted at the beginning and end, pointers and reference may still be valid.


Program example:

#include <iostream>#include <deque>#include <string>#include <algorithm>using namespace std;int main(){deque<string> strDeq;strDeq.assign(4,string("CHINA"));strDeq.push_back("first_string");strDeq.push_front("last_string");copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," "));cout << endl;cout << endl;for(int i = 0;i < strDeq.size();++i)cout << "strDeq[" << i << "] : " << strDeq[i] << endl;cout << endl;for(int i = 0;i < strDeq.size();++i)cout << "strDeq.at(" << i << ") : " << strDeq.at(i) << endl;cout << endl;strDeq.pop_front();strDeq.pop_back();copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," "));cout << endl;cout << endl;for(int i = 1;i < strDeq.size();++i)strDeq[i] = "pre" + strDeq[i];copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," "));cout << endl;cout << endl;strDeq.resize(4,"resized string");copy(strDeq.begin(),strDeq.end(),ostream_iterator<string>(cout," "));cout << endl;cout << endl;}

Running result:

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.