One of the STL series Deque bidirectional queues

Source: Internet
Author: User

The deque bidirectional queue is a continuous linear space with bidirectional openings that can efficiently insert and delete elements at both ends of the deque, which are very similar to vectors on the interface, and list the common member functions of deque:

The implementation of the deque is complex, and a map is maintained internally (note.) Not a map container in STL is a small contiguous space in which each element is a pointer to another (larger) region called a buffer, which is used to hold the data in the deque. Thus deque is slower than vector in random access and traversal of data. Specific Deque implementation can refer to the "STL source Analysis", of course, the book used in the SGI STL and VS2008 used in the PJ STL implementation method is still different. The following is a diagram of the deque structure:

Due to the length of the problem, the implementation of the details of the deque is no longer in-depth, the following given the use of deque example:

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

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

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

	Ideq.push_front (i);
	Output deque printf ("\ n output deque data: \ 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 tail ("\ n Delete data at the ends ... \ n");
	Ideq.pop_back ();

	Ideq.pop_front (); Output dEque printf ("\ n output deque data: \ n");
	for (pos = Ideq.begin (); Pos!= ideq.end (); pos++) printf ("%d", *pos);
	Putchar (' \ n ');
return 0; }

The results of the operation are as follows:

Also pay attention to a point. For deque and vectors, try to use less erase (POS) and Erase (Beg,end). Because this removes data in the middle, it causes the subsequent data to move forward, making it inefficient.

Reprint please indicate the source, the original address: http://blog.csdn.net/morewindows/article/details/6946811

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.