C ++ STL learning notes 3 deque dual-end queue container

Source: Internet
Author: User

/*
*
**************************************** ****
* Deque:
**************************************** ****
*
*
* Random access is allowed. elements are inserted and deleted in the ** header and tail **. the time complexity is O (1)
* Random Access container back insertion sequence front Insertion Sequence
*
**************************************** **************************************** ****
* Deque is more advantageous than vector when considering the memory allocation policies and operation performance of container elements *
* Map management block. The block contains a set of data, and the memory allocation is more detailed (in blocks, the second-level map is used for management )*
**************************************** **************************************** ****
*
* To use deque, you must use a macro statement # include <deque>
*
**************************************** **************************************** ******
*
* Create a deque object:
* 1. deque <int>;
* 2. deque <int> A (10); // object a with 10 elements. The default value of each element is 0.
* 3. deque <char> A (5, 'k ');
* 4. deque <char> B (a); // deque <char> C (A. Begin (), A. End ())
*
**************************************** **************************************** ******
*
* Initialization assignment
* Void push_back (const T & value)
*
**************************************** **************************************** ******
*
* Traversal
* Reference operator [] (size_type N)
* Iterator begin ()
* Iterator end ()
*
**************************************** **************************************** ******
*
* Common functions
*
* Bool empty ();
* Size_type size (); size_type max_size (); // No size_type capacity (); reverse ();
* Reference Front (); reference back ();
* Void pop_front (); void push_front (); // added to the vector
* Void pop_back (); void push_back ();
* Void clear ();
* // For some functions, see the Code such as erase ();
*
*
*
**************************************** ****
* Author: cumirror
* Email: tongjinooo@163.com
**************************************** ****
*
*/

# Include <iostream>
# Include <deque>

 

Int main ()
{
Using namespace STD;
Deque <int> A (10, 5 );

// Array
Cout <"array access:" <Endl;
A [0] = 100;
For (int s = 0; S <A. Size (); s ++ ){
Cout <A [s] <Endl;
}

// Iterator Method
Cout <"iterator access:" <Endl;
Deque <int >:: iterator I, iend;
I = A. Begin ();
Iend = A. End ();
* I = 101;
For (deque <int >:: iterator J = I; J! = Iend; j ++ ){
Cout <* j <Endl;
}

// Pay attention to the invalidation of the iterator during insertion and deletion. You can refer to the following web page for instructions but no reason is described, in fact, this has a lot to do with how various containers are implemented. If you want to study it, you can refer to STL source code analysis.
// Http://blog.csdn.net/jokenchang2000/archive/2008/07/01/2603485.aspx
Cout <"insert" <Endl;
A. insert (A. Begin (), 102 );

// Note when deleting a table. It is a semi-closed interval.
// Deletion of a single element of erase (iterator POS) is also supported.
Cout <"delete" <Endl;
A. Erase (A. Begin () + 4, A. Begin () + 6 );
For (deque <int >:: iterator K = A. Begin (); k! = A. End (); k ++ ){
Cout <* k <Endl;
}

// Insert the header
A. push_front (999 );

// Reverse Traversal
Cout <"reverse access" <Endl;
Deque <int >:: reverse_iterator Ri;
For (Ri = A. rbegin (); Ri! = A. rend (); ri ++ ){
Cout <* RI <Endl;
}

Deque <int> B;
B. push_back (4 );
B. push_back (5 );
B. push_front (3 );
B. push_front (2 );
Cout <"Interchange" <Endl;
B. Swap ();
For (int l = 0; L <A. Size (); L ++ ){
Cout <A [l] <Endl;
}
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.