A detailed description of STL's deque function

Source: Internet
Author: User

#include <deque>

The deque bidirectional queue is a continuous linear space with two-way openings that efficiently insert and delete elements at both ends of the deque and are very similar to vectors on the interface


Function Describe
C.assign (Beg,end)
C.assign (N,elem)
Assigns the data in the [beg; end] Interval to C.
Assigns a copy of n elem to C.
c.at (IDX) Returns the index IDX refers to the data, if the IDX is out of bounds, throws Out_of_range.
C.back () Returns the last data without checking to see if the data exists.
C.begin () Returns a data that is valued by the iterator.
C.clear () Removes all data from the container.
Deque<elem> C
Deque<elem> C1 (C2)
Deque<elem> C (N)
Deque<elem> c (n, Elem)
Deque<elem> C (beg,end)
C.~deque<elem> ()
Create an empty deque.
Copy a deque.
Creates a deque that contains n data, and the data is constructed by default.
Create a deque that contains n elem copies.
Create a deque with an interval of [beg;end].
Destroys all data and frees up memory.
C.empty () Determines whether the container is empty.
C.end () Points to the last data address in the iterator.
C.erase (POS)
C.erase (Beg,end)
Deletes data from the POS location and returns the location of the next data.
Delete the data from the [Beg,end] interval and return to the location of the next data.
C.front () Returns a data back to the ground.
Get_allocator Use the constructor to return a copy.
C.insert (Pos,elem)
C.insert (Pos,n,elem)
C.insert (Pos,beg,end)
Inserts a elem copy at the POS location and returns the new data location.
Inserts >n elem data at the POS location. no return value.
Inserts the data in the [Beg,end] interval at the POS location. no return value.
C.max_size () Returns the maximum number of data in a container.
C.pop_back () Delete the last data.
C.pop_front () Delete header data.
C.push_back (Elem) Add a data to the trailer.
C.push_front (Elem) Inserts a data into the head.
C.rbegin () Returns the first data for a reverse queue.
C.rend () Returns the next position of the last data in a reverse queue.
C.resize (num) Re-specify the length of the queue.
C.size () Returns the number of actual data in the container.
C1.swap (C2)
Swap (C1,C2)
Swaps the C1 and C2 elements.
Above operation.

Deque need to allocate space if your insert exceeds the deque temporary space capacity will reallocate a larger space to deque so it will be slower than the queue

/*deque: It's a double-ended queue,
1) Support immediately access, that is, the [] operator,
2) support Both sides operation, push (POP)-back (front), on both sides of the operation with the list efficiency is similar

Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, should generally follow the following principles:
1. If you need efficient immediate access, without the efficiency of insert and delete, use vector
2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the list
3, if you need to immediately access, and care about the end of data insertion and deletion, you should use Deque.
*/

http://blog.csdn.net/morewindows/article/details/6946811

Reference code from the above address

Bidirectional queue deque//by morewindows http://blog.csdn.net/morewindows #include <deque> #include <cstdio> #include &
Lt;algorithm> using namespace std; int main () {deque<int> Ideq (0);//create a deque Ideq with elements of default value Deque<int>::iter
	Ator POS;

	int i;
	
	Use assign () assignment assign in the computer is the meaning of the assignment for (i = 0; i <; ++i) ideq[i] = i;
	Output deque printf ("Data in output deque: \ n");
	for (i = 0; i <; ++i) printf ("%d", ideq[i]);

	Putchar (' \ n ');
	Add new data to printf ("\ n Add new data ... \ n" at the tail and tail);
	Ideq.push_back (100);

	Ideq.push_front (i);
	Output deque printf ("\ nthe data in output deque: \ n");
	for (pos = Ideq.begin (); pos! = Ideq.end (); pos++) printf ("%d", *pos);

	Putchar (' \ n ');
	Find const int findnumber = 19;
	printf ("\ n Find%d\n", findnumber);
	pos = Find (Ideq.begin (), Ideq.end (), findnumber);
	if (pos! = Ideq.end ()) printf ("Find%d success\n", *pos);

	else printf ("Find failed\n");
	Delete Data printf at the kinsoku ("\ n delete data ... \ n");
	Ideq.pop_back ();

	Ideq.pop_front (); Output dEque printf ("\ nthe data in output deque: \ n");
	for (pos = Ideq.begin (); pos! = Ideq.end (); pos++) printf ("%d", *pos);
	Putchar (' \ n ');
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.