"C + + STL" deques

Source: Internet
Author: User

1. Structure

Container deque and vectors are very similar, but also the use of dynamic arrays to manage elements, providing random access, with a vector almost the same interface, the difference is that the deque dynamic array is open and close, so can be quickly placed and deleted at the tail and back.

In order to acquire this ability, the deque usually acts as a set of independent chunks, the first chunk expands in one direction, and the last chunk expands toward the other.

2, deque Ability 2.1 and vector difference

Both sides can quickly insert and delete elements.

When accessing an element, the DEQUE internal structure will have an indirect process, so the access and iterator actions will be slightly slower.

Iterators need to jump in different chunks of memory, so it must be a special smart pointer, not a generic pointer.

Deque does not support the control of the capacity and memory redistribution timing, except that deleting or adding elements anywhere in the end will invalidate reference, pointers, iterators. Deque's memory allocation is better than vector, and it is known internally that Deque does not have to replicate all elements in memory reallocation.

Deque memory blocks are freed when they are no longer in use. The memory size of the deque can be reduced.

2.2 In common with vectors

Inserting and removing elements in the middle section is relatively slow, as all elements need to be moved or freed up to fill the space.

The iterator belongs to the random access iterator (discretionary access iterator).

2.3 Applicable Scenarios

Need to insert or delete elements in two sections

No reference to elements within a container

Requires releasing elements that are no longer in use.

3. Operation function 3.1 constructors and destructors

Operation

Effect

Deque<elem> C

Produce an empty deque.

Deque<elem> C1 (C2)

Generates a copy for a deque (all elements are copied)

Deque<elem> C (N)

Produces a deque containing n elements that are generated with the default constructor.

Deque<elem> C (N,elem)

Produces a deque containing n elements, all of which are copies of a elem

Deque<elem> C (beg,end)

Generates a deque with an element in the interval [beg,end] as the initial value

C.~deque<elem> ()

Destroy all elements and free up memory

3.2 Non-change operation

Operation

Effect

C.size ()

Returns the current number of elements

C.empty ()

Determines whether the size is zero, equal to 0 = = size (), higher efficiency

C.max_size ()

Returns the maximum number of elements that can be accommodated

C1 = = C2

Determine if C1 equals C2

C1! = C2

Determine if C1 is not equal to C2 (equal to!) ( C1==C2))

C1 < C2

Determine if C1 is less than C2

C1 > C2

Determine if C1 is greater than C2

C1 <= C2

Determine if C1 is less than or equal to C2 (equal to!) ( C2<C1))

C1 >= C2

Determine if C1 is greater than or equal to C2 (equal to!) ( C1<C2))

c.at (IDX)

Returns the element indicated by the index IDX, if the IDX is out of bounds, throws Out_of_range

C[IDX]

Returns the element indicated by the index IDX, without a range check

C.front ()

Returns the first element without checking the existence of the first element

C.back ()

Returns the last element without checking whether the last element exists

C.begin ()

Returns a random access iterator that points to the first element

C.end ()

Returns a random access iterator that points to the next position of the last element

C.rbegin ()

Returns a reverse iterator that points to the first element of a reverse iteration

C.rend ()

Returns a reverse iterator that points to the next position of the last element of the reverse iteration

3.3 Volatile operation

Operation

Effect

C1 = C2

Assigns all elements of C2 to C1

C.assign (N,elem)

Assigns a copy of n elem elements to C

C.assign (Beg,end)

Assigns all elements of the interval [beg,end] to C

C1.swap (C2)

C1 and C2 Interchange of elements

Swap (C1,C2)

C1 and C2 elements interchange, global functions

C.insert (Pos,elem)

Place a elem copy in the POS location, returning the location of the new element

C.insert (Pos,n,elem)

n elem copies are placed at POS location, no return value

C.insert (Pos,beg,end)

Place a copy of all elements in the POS location [beg,end], no return value

C.push_back (Elem)

Add a elem copy at the tail

C.pop_back ()

Remove the last element

C.push_front (Elem)

Add a copy of the Elem element to the head

C.pop_front ()

Removing head elements

C.erase (POS)

Removes an element from the POS position, returning the position of the next element

C.erase (Beg,end)

Removes all elements of the interval [beg,end], returning the position of the next element

C. Resize (num)

Resets the size (number of elements) to num, and if size () increases, the new element is constructed with the default constructor

C.resize (num, elem)

Resets the size (number of elements) to num, and if size () increases, the new element is a copy of Elem

C.clear ()

Remove all elements, empty containers

3.4 Note

Deque does not provide capacity operation (capacity (), reserve ())

Deque provides a function between the completion of the head deletion (Pop_front ()) and the insertion (push_front ()) operation.

In addition to at (), no member function checks whether the index or iterator is valid.

The insertion and deletion of elements can result in memory reallocation, so any insertions and deletions can invalidate all pointers, reference, iterators that point to the deque element. The only exception is the insertion of elements in the head or tail, pointers and reference are still valid, but iterators will fail.

4. Sample Code
//cont/deque1. cpp#include<iostream>#include<deque>#include<string>#include<algorithm>using namespacestd; intMain () {//Create empty deque of stringsdeque<string>Coll; //Insert several elementsColl.assign (3,string("string")); Coll.push_back ("Last string"); Coll.push_front ("First string"); //print elements separated by newlinescopy (Coll.begin (), Coll.end (), Ostream_iterator<string> (cout,"\ n")); cout<<Endl; //Remove First and last elementColl.pop_front ();       Coll.pop_back (); //insert ' another ' into every element but the first        for(intI=1; I<coll.size (); ++i) {Coll[i]="another"+Coll [i]; }       //Change size to four elementsColl.resize (4,"resized string"); //print elements separated by newlinescopy (Coll.begin (), Coll.end (), Ostream_iterator<string> (cout,"\ n")); }

Output:

   string   string   string   string      string   string        string   string   

"C + + STL" deques

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.