"C + + STL learning three" container deque in-depth study __c++

Source: Internet
Author: User

C + + STL container deque is similar to vector, and also uses a dynamic array to manage elements.

You need to include the header file before using deque:

#include <deque>

It is a class template defined within a namespace std:

Template<class _ty,
Class _ax = allocator<_ty> >
Class Deque;

The first template parameter is used to represent the element type, and the second is optional, specifying the memory model. The default memory model is generally used.

Unlike vectors, the dynamic array of deque is open to both end and end, enabling fast insertion and deletion at both end and end.

the logical structure of the deque:



the internal structure of the deque

Deque is an optimized basic sequence container for adding and removing elements on both ends of a sequence. Usually consists of a number of independent blocks, the first block in a certain direction expansion, the last block to the other side to expand. It allows for faster random access but it does not keep all objects in a contiguous block of memory like a vector, but rather multiple contiguous chunks of memory. And keep track of these blocks and sequences in a mapping structure.

Its internal structure is shown in the following illustration:


characteristics of Deque:

1, support random access, that is, support [] and at (), but the performance is not vector good.

2, can insert and delete inside the operation, but the performance is not as list.


the difference between the deque and the vector:

1, both ends can quickly insert and delete elements. Vectors can only be performed at the tail end.

2, deque element access and iterator operations will be slightly slower. Because the internal structure of the deque will be one more indirect process.

3. Iterators are special smart pointers, not generic pointers. It needs to jump between the different blocks.

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

5, does not support the capacity and memory allocation timing control.

Note: Inserting and deleting elements in addition to both ends will cause any pointers, references, and iterators that point to the deque element to fail. However, Deque's memory redistribution is better than vector. Because its internal structure shows that you do not need to copy all the elements.

6, deque memory block is no longer in use, will be released. The memory size of the deque can be reduced. However, this is not done and how it is defined by the implementation version.

deque characteristics similar to vectors:

1. Inserting and deleting elements in the middle section is relatively slow because all elements are moved.

2, the iterator belongs to the immediately access iterator.

It is best to adopt the Deque case:

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

2, do not need to refer to the elements inside the container.

3. Require the container to release elements that are no longer in use.

Deque's action function

Constructors and destructors:

Non-volatile operation:

Change of Operation:



Deque's operations are only two points different from the vector:

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

Deque directly provides functions to complete the insertion and deletion of the elements.

Others are the same as vectors. Attention:

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

2. Insertion and deletion of elements may result in memory reallocation. So any insert or delete operation will invalidate all pointers, reference, and iterators that point to the deque element. The only exception is that pointers and reference may still be valid after inserting the elements at the end.


program Example:

#include <iostream> #include <deque> #include <string> #include <algorithm> using namespace std;

	int main () {deque<string> strdeq;
	Strdeq.assign (4,string ("the"));
	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] << E
	Ndl
	
	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; }

Run Result:


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.