C++deque two-terminal queue __c++

Source: Internet
Author: User

Call header file: #include <deque>

using namespace Std;

Detailed usage (part):

Deque<type> K; ------defines a deque variable (which was initialized at the time of definition) such as: Deque<int> K;

K.empty ()------See if the example is empty, return 1, not return 0

K.clear ()------Clears all data in the queue

K.push_front (i)------add element I (queue size not preset) from front of existing elements

K.push_back (i)------add element I (queue size not preset) from behind existing elements

K.pop_front ()------Clear the first element

K.pop_back ()------Clear the last element

K.front ()------Display the first element, such as N = K.front ();

K.back ()------Display the last element

K.size ()------Output The number of existing elements


#include <stdio.h>
#include <deque>
using namespace std;
int main (void)
{
	int i;
	int a[10] = {0,1,2,3,4,5,6,7,8,9};
	deque<int> Q;
	for (i=0;i<=9;i++)
	{
		if (i%2==0)
			Q.push_front (a[i));
		else
			Q.push_back (a[i]);
	}                                  /* At this time the contents of the queue is: {8,6,4,2,0,1,3,5,7,9}*/
	q.pop_front ();
	printf ("%d\n", Q.front ());    /* Clears the first element and outputs the first (6)/
	q.pop_back ();
	printf ("%d\n", Q.back ());     /* Clears the last element and outputs the last (7) */return
	0;
}


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.