Ordered Collections

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. Brian Kernighan

deque

Language C++ | Level Intermediate | Category Standard Template Library (STL) | 2015-11-16 22:40:16

0 Comments


Definition

Double-ended queues or dequeue is a container with dynamic sizes. It can be expanded on both ends.

Character

Double-ended queue can be push or pop from both sides. Dynamic size.
Support efficient insertion and deletion of elements.
The elements of a deque are not stored contiguously.

source Code

								
#include "stdafx.h"

#include < deque >
#include < iostream >
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	deque < int > deq;

	cout << "Store items in deque" << endl;

	for (int i = 0; i < 10; i++)
		deq.push_front(i);

	deque < int >::iterator iter;
	cout << "Display items from deque : ";
	for (iter = deq.begin(); iter != deq.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	cout << "Pop back and front items from deque " << endl;
	deq.pop_front();
	deq.pop_back();
	cout << "Display items from deque : ";
	for (iter = deq.begin(); iter != deq.end(); ++iter)
		cout << *iter << " ";
	cout << endl;


	return 0;
}

						

Output

    					 
					Store items in deque
					Display items from deque : 9 8 7 6 5 4 3 2 1 0
					Pop back and front items from deque
					Display items from deque : 8 7 6 5 4 3 2 1

					    					 
					    

When to use

If you want insert the items both ends, you can use dequeue.

0 Comments



Please login to add comments.