C++ deques

deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/64842ee5-057a-4063-8c16-4267a0332584.htm
Constructors 构造函数   
assign() 元素赋值   
at() 返回指定位置的元素   
back() 返回最末一个元素   
begin() 返回第一个元素的迭代器   
capacity() 返回vector所能容纳的元素数量(在不重新分配内存的情况下)   
clear() 清空所有元素   
empty() 判断是否为空(返回true时为空)   
end() 返回最末元素的迭代器(译注:实指向最末元素的下一个位置)   
erase() 删除指定元素   
front() 返回第一个元素   
get_allocator() 返回的内存分配器   
insert() 插入元素到中   
max_size() 返回所能容纳元素的最大数量(上限)   
pop_back() 移除最后一个元素   
push_back() 在最后添加一个元素   
push_front()将一个元素添加到双端队列的开头
rbegin() 返回尾部的逆迭代器   
rend() 返回起始的逆迭代器   
reserve() 设置最小的元素容纳数量   
resize() 改变元素数量的大小   
size() 返回元素数量的大小   
swap() 交换   



#include <deque>
#include <iostream>

//构造函数
void DequeConstructor(void);

//元素赋值 
void Deque_assign(void);

//返回指定位置的元素
void Deque_at(void);

//返回最末一个元素 
void Deque_back(void);

//返回第一个元素的迭代器 
void Deque_begin(void);

//清空所有元素
void Deque_clear(void);

//判断是否为空(返回true时为空)
void Deque_empty(void);

//返回最末元素的迭代器(译注:实指向最末元素的下一个位置)
void Deque_end(void);

//删除指定元素 
void Deque_erase(void);

//返回第一个元素
void Deque_front(void);

//返回内存分配器 
void Deque_get_allocator(void);

//插入元素到
void Deque_insert(void);

//返回所能容纳元素的最大数量(上限)
void Deque_max_size(void);

//移除最后一个元素
void Deque_pop_back(void);

//在最后添加一个元素
void Deque_push_back(void);

//将一个元素添加到双端队列的开头
void Deque_push_front(void);

//返回尾部的逆迭代器
void Deque_rbegin(void);

//返回起始的逆迭代器
void Deque_rend(void);

//改变元素数量的大小
void Deque_resize(void);

//返回元素数量的大小
void Deque_size(void);

//交换
void Deque_swap(void);

int main()
{
	//DequeConstructor();
	//Deque_assign();
	//Deque_at();
	//Deque_back();
	//Deque_begin();
	//Deque_clear();
	//Deque_empty();
	//Deque_end();
	//Deque_erase();
	//Deque_front();
	//Deque_get_allocator();
	//Deque_insert();
	//Deque_max_size();
	//Deque_pop_back();
	//Deque_push_back();
	//Deque_push_front();
	//Deque_rbegin();
	//Deque_rend();
	//Deque_resize();
	//Deque_size();
	Deque_swap();
	return 0;
}

//构造函数
void DequeConstructor(void)
{
	using namespace std;
	deque <int>::iterator c1_Iter, c2_Iter, c3_Iter, c4_Iter, c5_Iter, c6_Iter;

	// Create an empty deque c0
	deque <int> c0;

	// Create a deque c1 with 3 elements of default value 0
	deque <int> c1(3);

	// Create a deque c2 with 5 elements of value 2
	deque <int> c2(5, 2);

	// Create a deque c3 with 3 elements of value 1 and with the 
	// allocator of deque c2
	deque <int> c3(3, 1, c2.get_allocator());

	// Create a copy, deque c4, of deque c2
	deque <int> c4(c2);

	// Create a deque c5 by copying the range c4[_First, _Last)
	c4_Iter = c4.begin();
	c4_Iter++;
	c4_Iter++;
	deque <int> c5(c4.begin(), c4_Iter);

	// Create a deque c6 by copying the range c4[_First, _Last) and 
	// c2 with the allocator of deque
	c4_Iter = c4.begin();
	c4_Iter++;
	c4_Iter++;
	c4_Iter++;
	deque <int> c6(c4.begin(), c4_Iter, c2.get_allocator());

	cout << "c1 = ";
	for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
		cout << *c1_Iter << " ";
	cout << endl;

	cout << "c2 = ";
	for (c2_Iter = c2.begin(); c2_Iter != c2.end(); c2_Iter++)
		cout << *c2_Iter << " ";
	cout << endl;

	cout << "c3 = ";
	for (c3_Iter = c3.begin(); c3_Iter != c3.end(); c3_Iter++)
		cout << *c3_Iter << " ";
	cout << endl;

	cout << "c4 = ";
	for (c4_Iter = c4.begin(); c4_Iter != c4.end(); c4_Iter++)
		cout << *c4_Iter << " ";
	cout << endl;

	cout << "c5 = ";
	for (c5_Iter = c5.begin(); c5_Iter != c5.end(); c5_Iter++)
		cout << *c5_Iter << " ";
	cout << endl;

	cout << "c6 = ";
	for (c6_Iter = c6.begin(); c6_Iter != c6.end(); c6_Iter++)
		cout << *c6_Iter << " ";
	cout << endl;
	return;
	/*
	c1 = 0 0 0
	c2 = 2 2 2 2 2
	c3 = 1 1 1
	c4 = 2 2 2 2 2
	c5 = 2 2
	c6 = 2 2 2
	请按任意键继续. . .
	*/
}

//元素赋值 
void Deque_assign(void)
{
	using namespace std;
	deque <int> c1, c2;
	deque <int>::const_iterator cIter;

	c1.push_back(10);
	c1.push_back(20);
	c1.push_back(30);
	c2.push_back(40);
	c2.push_back(50);
	c2.push_back(60);

	cout << "c1 =";
	for (cIter = c1.begin(); cIter != c1.end(); cIter++)
		cout << " " << *cIter;
	cout << endl;

	c1.assign(++c2.begin(), c2.end());
	cout << "c1 =";
	for (cIter = c1.begin(); cIter != c1.end(); cIter++)
		cout << " " << *cIter;
	cout << endl;

	c1.assign(7, 4);
	cout << "c1 =";
	for (cIter = c1.begin(); cIter != c1.end(); cIter++)
		cout << " " << *cIter;
	cout << endl;

	return;
	/*
	c1 = 10 20 30
	c1 = 50 60
	c1 = 4 4 4 4 4 4 4
	请按任意键继续. . .
	*/
}

//返回指定位置的元素
void Deque_at(void)
{
	using namespace std;
	deque <int> c1;

	c1.push_back(10);
	c1.push_back(20);

	const int& i = c1.at(0);
	int& j = c1.at(1);
	cout << "The first element is " << i << endl;
	cout << "The second element is " << j << endl;

	return;
	/*
	The first element is 10
	The second element is 20
	请按任意键继续. . .
	*/
}

//返回最末一个元素 
void Deque_back(void)
{
	using namespace std;
	deque <int> c1;

	c1.push_back(10);
	c1.push_back(11);

	int& i = c1.back();
	const int& ii = c1.front();

	cout << "The last integer of c1 is " << i << endl;
	i--;
	cout << "The next-to-last integer of c1 is " << ii << endl;

	return;
	/*
	The last integer of c1 is 11
	The next-to-last integer of c1 is 10
	请按任意键继续. . .
	*/
}

//返回第一个元素的迭代器 
void Deque_begin(void)
{
	using namespace std;
	deque <int> c1;
	deque <int>::iterator c1_Iter;
	deque <int>::const_iterator c1_cIter;

	c1.push_back(1);
	c1.push_back(2);

	c1_Iter = c1.begin();
	cout << "The first element of c1 is " << *c1_Iter << endl;

	*c1_Iter = 20;
	c1_Iter = c1.begin();
	cout << "The first element of c1 is now " << *c1_Iter << endl;

	// The following line would be an error because iterator is const
	// *c1_cIter = 200;

	return;
	/*
	The first element of c1 is 1
	The first element of c1 is now 20
	请按任意键继续. . .
	*/
}

//清空所有元素
void Deque_clear(void)
{
	using namespace std;
	deque <int> c1;

	c1.push_back(10);
	c1.push_back(20);
	c1.push_back(30);

	cout << "The size of the deque is initially " << c1.size() << endl;
	c1.clear();
	cout << "The size of the deque after clearing is " << c1.size() << endl;

	return;
	/*
	The size of the deque is initially 3
	The size of the deque after clearing is 0
	请按任意键继续. . .
	*/
}

//判断是否为空(返回true时为空)
void Deque_empty(void)
{
	using namespace std;
	deque <int> c1;

	c1.push_back(10);
	if (c1.empty())
		cout << "The deque is empty." << endl;
	else
		cout << "The deque is not empty." << endl;

	return;
	/*
	The deque is not empty.
	请按任意键继续. . .
	*/
}

//返回最末元素的迭代器(译注:实指向最末元素的下一个位置)
void Deque_end(void)
{
	using namespace std;
	deque <int> c1;
	deque <int>::iterator c1_Iter;

	c1.push_back(10);
	c1.push_back(20);
	c1.push_back(30);

	c1_Iter = c1.end();
	c1_Iter--;
	cout << "The last integer of c1 is " << *c1_Iter << endl;

	c1_Iter--;
	*c1_Iter = 400;
	cout << "The new next-to-last integer of c1 is " << *c1_Iter << endl;

	// If a const iterator had been declared instead with the line:
	// deque <int>::const_iterator c1_Iter;
	// an error would have resulted when inserting the 400

	cout << "The deque is now:";
	for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
		cout << " " << *c1_Iter;

	cout << endl;
	return;
	/*
	The last integer of c1 is 30
	The new next-to-last integer of c1 is 400
	The deque is now: 10 400 30
	请按任意键继续. . .
	*/
}

//删除指定元素 
void Deque_erase(void)
{
	using namespace std;
	deque <int> c1;
	deque <int>::iterator Iter;

	c1.push_back(10);
	c1.push_back(20);
	c1.push_back(30);
	c1.push_back(40);
	c1.push_back(50);
	cout << "The initial deque is: ";
	for (Iter = c1.begin(); Iter != c1.end(); Iter++)
		cout << *Iter << " ";
	cout << endl;
	c1.erase(c1.begin());
	cout << "After erasing the first element, the deque becomes:  ";
	for (Iter = c1.begin(); Iter != c1.end(); Iter++)
		cout << *Iter << " ";
	cout << endl;
	Iter = c1.begin();
	Iter++;
	c1.erase(Iter, c1.end());
	cout << "After erasing all elements but the first, deque becomes: ";
	for (Iter = c1.begin(); Iter != c1.end(); Iter++)
		cout << *Iter << " ";
	cout << endl;

	return;
	/*
	The initial deque is: 10 20 30 40 50
	After erasing the first element, the deque becomes:  20 30 40 50
	After erasing all elements but the first, deque becomes: 20
	请按任意键继续. . .
	*/
}

//返回第一个元素
void Deque_front(void)
{
	using namespace std;
	deque <int> c1;

	c1.push_back(10);
	c1.push_back(11);

	int& i = c1.front();
	const int& ii = c1.front();

	cout << "The first integer of c1 is " << i << endl;
	i++;
	cout << "The second integer of c1 is " << ii << endl;
	return;
	/*
	The first integer of c1 is 10
	The second integer of c1 is 11
	请按任意键继续. . .
	*/
}

//返回内存分配器 
void Deque_get_allocator(void)
{

	using namespace std;
	// The following lines declare objects that use the default allocator.
	deque <int> c1;
	deque <int, allocator<int> > c2 = deque <int, allocator<int> >(allocator<int>());

	// c3 will use the same allocator class as c1
	deque <int> c3(c1.get_allocator());

	deque <int>::allocator_type xlst = c1.get_allocator();
	// You can now call functions on the allocator class used by c1

	return;
}

//插入元素到
void Deque_insert(void)
{
	using namespace std;
	deque <int> c1, c2;
	deque <int>::iterator Iter;

	c1.push_back(10);
	c1.push_back(20);
	c1.push_back(30);
	c2.push_back(40);
	c2.push_back(50);
	c2.push_back(60);

	cout << "[ c1 = ";
	for (Iter = c1.begin(); Iter != c1.end(); Iter++)
		cout << *Iter << " ";
	cout << "]" << endl;

	Iter = c1.begin();
	Iter++;
	c1.insert(Iter, 100);
	cout << "[ c1 = ";
	for (Iter = c1.begin(); Iter != c1.end(); Iter++)
		cout << *Iter << " ";
	cout << "]" << endl;

	Iter = c1.begin();
	Iter++;
	Iter++;
	c1.insert(Iter, 2, 200);

	cout << "[ c1 = ";
	for (Iter = c1.begin(); Iter != c1.end(); Iter++)
		cout << *Iter << " ";
	cout << "]" << endl;

	c1.insert(++c1.begin(), c2.begin(), --c2.end());

	cout << "[ c1 = ";
	for (Iter = c1.begin(); Iter != c1.end(); Iter++)
		cout << *Iter << " ";
	cout << "]" << endl;

	return;
	/*
	[ c1 = 10 20 30 ]
	[ c1 = 10 100 20 30 ]
	[ c1 = 10 100 200 200 20 30 ]
	[ c1 = 10 40 50 100 200 200 20 30 ]
	请按任意键继续. . .
	*/
}

//返回所能容纳元素的最大数量(上限)
void Deque_max_size(void)
{
	using namespace std;
	deque <int> c1;
	deque <int>::size_type i;

	i = c1.max_size();
	cout << "The maximum possible length of the deque is " << i << "." << endl;

	return;
	/*
	The maximum possible length of the deque is 1073741823.
	请按任意键继续. . .
	*/
}

//移除最后一个元素
void Deque_pop_back(void)
{
	using namespace std;
	deque <int> c1;

	c1.push_back(1);
	c1.push_back(2);
	cout << "The first element is: " << c1.front() << endl;
	cout << "The last element is: " << c1.back() << endl;

	c1.pop_back();
	cout << "After deleting the element at the end of the deque, the "
		"last element is: " << c1.back() << endl;

	return;
	/*
	The first element is: 1
	The last element is: 2
	After deleting the element at the end of the deque, the last element is: 1
	请按任意键继续. . .
	*/
}

//在最后添加一个元素
void Deque_push_back(void)
{
	using namespace std;
	deque <int> c1;

	c1.push_back(1);
	if (c1.size() != 0)
		cout << "Last element: " << c1.back() << endl;

	c1.push_back(2);
	if (c1.size() != 0)
		cout << "New last element: " << c1.back() << endl;

	return;
	/*
	Last element: 1
	New last element: 2
	请按任意键继续. . .
	*/
}

//将一个元素添加到双端队列的开头
void Deque_push_front(void)
{
	using namespace std;
	deque <int> c1;

	c1.push_front(1);
	if (c1.size() != 0)
		cout << "First element: " << c1.front() << endl;

	c1.push_front(2);
	if (c1.size() != 0)
		cout << "New first element: " << c1.front() << endl;

	return;
	/*
	First element: 1
	New first element: 2
	请按任意键继续. . .
	*/
}

//返回尾部的逆迭代器
void Deque_rbegin(void)
{

	using namespace std;
	deque <int> c1;
	deque <int>::iterator c1_Iter;
	deque <int>::reverse_iterator c1_rIter;

	// If the following line had replaced the line above, an error 
	// would have resulted in the line modifying an element 
	// (commented below) because the iterator would have been const
	// deque <int>::const_reverse_iterator c1_rIter;

	c1.push_back(10);
	c1.push_back(20);
	c1.push_back(30);

	c1_rIter = c1.rbegin();
	cout << "Last element in the deque is " << *c1_rIter << "." << endl;

	cout << "The deque contains the elements: ";
	for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
		cout << *c1_Iter << " ";
	cout << "in that order.";
	cout << endl;

	// rbegin can be used to iterate through a deque in reverse order
	cout << "The reversed deque is: ";
	for (c1_rIter = c1.rbegin(); c1_rIter != c1.rend(); c1_rIter++)
		cout << *c1_rIter << " ";
	cout << endl;

	c1_rIter = c1.rbegin();
	*c1_rIter = 40;  // This would have caused an error if a 
	// const_reverse iterator had been declared as 
	// noted above
	cout << "Last element in deque is now " << *c1_rIter << "." << endl;

	return;
	/*
	Last element in the deque is 30.
	The deque contains the elements: 10 20 30 in that order.
	The reversed deque is: 30 20 10
	Last element in deque is now 40.
	请按任意键继续. . .
	*/
}

//返回起始的逆迭代器
void Deque_rend(void)
{
	using namespace std;

	deque <int> c1;
	deque <int>::iterator c1_Iter;
	deque <int>::reverse_iterator c1_rIter;
	// If the following line had replaced the line above, an error
	// would have resulted in the line modifying an element
	// (commented below) because the iterator would have been const
	// deque <int>::const_reverse_iterator c1_rIter;

	c1.push_back(10);
	c1.push_back(20);
	c1.push_back(30);

	c1_rIter = c1.rend();
	c1_rIter--; // Decrementing a reverse iterator moves it forward 
	// in the deque (to point to the first element here)
	cout << "The first element in the deque is: " << *c1_rIter << endl;

	cout << "The deque is: ";
	for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
		cout << *c1_Iter << " ";
	cout << endl;

	// rend can be used to test if an iteration is through all of 
	// the elements of a reversed deque
	cout << "The reversed deque is: ";
	for (c1_rIter = c1.rbegin(); c1_rIter != c1.rend(); c1_rIter++)
		cout << *c1_rIter << " ";
	cout << endl;

	c1_rIter = c1.rend();
	c1_rIter--; // Decrementing the reverse iterator moves it backward 
	// in the reversed deque (to the last element here)
	*c1_rIter = 40; // This modification of the last element would 
	// have caused an error if a const_reverse 
	// iterator had been declared (as noted above)
	cout << "The modified reversed deque is: ";
	for (c1_rIter = c1.rbegin(); c1_rIter != c1.rend(); c1_rIter++)
		cout << *c1_rIter << " ";
	cout << endl;

	return;
	/*
	The first element in the deque is: 10
	The deque is: 10 20 30
	The reversed deque is: 30 20 10
	The modified reversed deque is: 30 20 40
	请按任意键继续. . .
	*/
}

//改变元素数量的大小
void Deque_resize(void)
{
	using namespace std;
	deque <int> c1;

	c1.push_back(10);
	c1.push_back(20);
	c1.push_back(30);

	c1.resize(4, 40);
	cout << "The size of c1 is: " << c1.size() << endl;
	cout << "The value of the last element is " << c1.back() << endl;

	c1.resize(5);
	cout << "The size of c1 is now: " << c1.size() << endl;
	cout << "The value of the last element is now " << c1.back() << endl;

	c1.resize(2);
	cout << "The reduced size of c1 is: " << c1.size() << endl;
	cout << "The value of the last element is now " << c1.back() << endl;

	return;
	/*
	The size of c1 is: 4
	The value of the last element is 40
	The size of c1 is now: 5
	The value of the last element is now 0
	The reduced size of c1 is: 2
	The value of the last element is now 20
	请按任意键继续. . .
	*/
}

//返回元素数量的大小
void Deque_size(void)
{
	using namespace std;
	deque <int> c1;
	deque <int>::size_type i;

	c1.push_back(1);
	i = c1.size();
	cout << "The deque length is " << i << "." << endl;

	c1.push_back(2);
	i = c1.size();
	cout << "The deque length is now " << i << "." << endl;
	return;
	/*
	The deque length is 1.
	The deque length is now 2.
	请按任意键继续. . .
	*/
}

//交换
void Deque_swap(void)
{

	using namespace std;
	deque <int> c1, c2, c3;
	deque <int>::iterator c1_Iter;

	c1.push_back(1);
	c1.push_back(2);
	c1.push_back(3);
	c2.push_back(10);
	c2.push_back(20);
	c3.push_back(100);

	cout << "The original deque c1 is:";
	for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
		cout << " " << *c1_Iter;
	cout << endl;

	c1.swap(c2);

	cout << "After swapping with c2, deque c1 is:";
	for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
		cout << " " << *c1_Iter;
	cout << endl;

	swap(c1, c3);

	cout << "After swapping with c3, deque c1 is:";
	for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
		cout << " " << *c1_Iter;
	cout << endl;

	swap<>(c1, c2);
	cout << "After swapping with c2, deque c1 is:";
	for (c1_Iter = c1.begin(); c1_Iter != c1.end(); c1_Iter++)
		cout << " " << *c1_Iter;
	cout << endl;

	return;
	/*
	The original deque c1 is: 1 2 3
	After swapping with c2, deque c1 is: 10 20
	After swapping with c3, deque c1 is: 100
	After swapping with c2, deque c1 is: 1 2 3
	请按任意键继续. . .
	*/
}


你可能感兴趣的:(C++ deques)