[C++ container] deque

1. The basic concept of deque container

deque is the abbreviation of double-ended queue, also known as double-ended queue container.

Features:

  • Double-ended array, you can insert and delete the head end

The difference between deque and vector:

  • The vector is inefficient for the insertion and deletion of the header. The larger the amount of data, the lower the efficiency.

  • Relatively speaking, deque inserts and deletes the head faster than vector

  • The speed of vector access to elements is faster than deque, which is related to the internal implementation of the two

The internal working principle of deque:

There is a central controller inside deque , which maintains the contents of each buffer, and the real data is stored in the buffer

The central controller maintains the address of each buffer, making it like a continuous memory space when using deque

The iterator of the deque container also supports random access

2. Deque constructor

Function description:

  • deque container structure

Function prototype:

  • deque<T> deqT; //Default construction form

  • deque(beg, end); //The constructor copies the elements in the [beg, end) range to itself.

  • deque(n, elem); //The constructor copies n elems to itself.

  • deque(const deque &deq); //Copy constructor

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


/*

deque<T> deqT;                      //默认构造形式
deque(beg, end);                  //构造函数将[beg, end)区间中的元素拷贝给本身。
deque(n, elem);                    //构造函数将n个elem拷贝给本身。
deque(const deque &deq);   //拷贝构造函数
*/

//打印函数  只读
void printDeque1(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//容器里面的数据不可用修改
		//(*it) = 100;
		cout << (*it) << "\t";
	}
	cout << endl;
}

void test01()
{
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	//打印
	printDeque1(d1);

	deque<int>d2(d1.begin(), d1.end());    //区间构造
	printDeque1(d2);

	deque<int>d3(10, 100);   //10个100
	printDeque1(d3);

	deque<int>d4(d3);        //拷贝构造
	printDeque1(d4);

}

int main(void)
{
	test01();
	system("pause");
	return 0;
}

3. Deque assignment operation

Function description:

  • Assign a value to the deque container

Function prototype:

  • deque& operator=(const deque &deq); //Overload the equal sign operator

  • assign(beg, end); // Assign a copy of the data in the interval [beg, end) to itself.

  • assign(n, elem); //Assign n copies of elem to itself.

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


/*
deque& operator=(const deque &deq);          //重载等号操作符
assign(beg, end);                            //将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);                             //将n个elem拷贝赋值给本身

*/

//打印函数  只读
void printDeque2(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//容器里面的数据不可用修改
		//(*it) = 100;
		cout << (*it) << "\t";
	}
	cout << endl;
}

void test02()
{
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	printDeque2(d1);

	deque<int>d2;     //等号赋值
	d2 = d1;
	printDeque2(d2);

	//assin赋值
	deque<int>d3;
	d3.assign(d1.begin(), d1.end());
	printDeque2(d3);


	deque<int>d4;
	d4.assign(10, 100);
	printDeque2(d4);
}

int main(void)
{
	test02();
	system("pause");
	return 0;
}

4. Deque size operation

Function description:

  • Operate on the size of the deque container

Function prototype:

  • deque.empty(); //Determine whether the container is empty

  • deque.size(); //Returns the number of elements in the container

  • deque.resize(num); //Re-specify the length of the container as num. If the container becomes longer, the new position will be filled with the default value.

                                     //If the container becomes shorter, the elements at the end that exceed the length of the container are deleted.

  • deque.resize(num, elem); //Re-specify the length of the container as num. If the container becomes longer, fill the new position with the elem value.

                                                 //If the container becomes shorter, the elements at the end that exceed the length of the container are deleted.

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


/*
deque.empty();                    //判断容器是否为空
deque.size();                     /返回容器中元素的个数
deque.resize(num);                //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
                                  //如果容器变短,则末尾超出容器长度的元素被删除。

deque.resize(num, elem);          //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
                                  //如果容器变短,则末尾超出容器长度的元素被删除。

*/

//打印函数  只读
void printDeque3(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//容器里面的数据不可用修改
		//(*it) = 100;
		cout << (*it) << " ";
	}
	cout << endl;
}

void test03()
{
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	printDeque3(d1);

	if (d1.empty())
	{
		cout << "d1为空" << endl;
	}
	else
	{
		cout << "d1不为空" << endl;
		cout << "d1的大小是:" << d1.size() << endl;
		//deque容器没有容量的概念
	}

	//重新指定大小
	d1.resize(15);        //用0填充
	printDeque3(d1);

	d1.resize(20,1);        //用1填充
	printDeque3(d1);

	d1.resize(5);
	printDeque3(d1);
}

int main(void)
{
	test03();
	system("pause");
	return 0;
}

to sum up:

  • deque has no concept of capacity

  • Determine whether it is empty --- empty

  • Return the number of elements --- size

  • Re-specify the number --- resize

5. Deque insertion and deletion

Function description:

  • Insert and delete data into the deque container

 

Function prototype:

Insert operation at both ends:

  • push_back(elem); //Add a data at the end of the container

  • push_front(elem); //Insert a data in the head of the container

  • pop_back(); //Delete the last data of the container

  • pop_front(); //Delete the first data of the container

Specify location operation:

  • insert(pos,elem); //Insert a copy of the elem element at position pos and return the position of the new data.

  • insert(pos,n,elem); //Insert n elem data at position pos, no return value.

  • insert(pos,beg,end); //Insert the data in the [beg,end) interval at position pos, no return value.

  • clear(); //Empty all data in the container

  • erase(beg,end); //Delete the data in the [beg,end) interval and return the position of the next data.

  • erase(pos); //Delete the data at position pos and return to the position of the next data.

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


/*
两端插入操作:
push_back(elem);         //在容器尾部添加一个数据
push_front(elem);        //在容器头部插入一个数据
pop_back();              //删除容器最后一个数据
pop_front();             //删除容器第一个数据


*/


//打印函数  只读
void printDeque4(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//容器里面的数据不可用修改
		//(*it) = 100;
		cout << (*it) << " ";
	}
	cout << endl;
}


void test04()
{
	deque<int>d1;

	//尾插
	d1.push_back(10);
	d1.push_back(20);

	//头插
	d1.push_front(100);
	d1.push_front(200);

	//打印
	printDeque4(d1);    //200 100 10 20 

	//尾删
	d1.pop_back();
	printDeque4(d1);    //200 100 10

	//头删
	d1.pop_front();
	printDeque4(d1);    //100 10
}


/*
指定位置操作:

insert(pos,elem);       //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
insert(pos,n,elem);     //在pos位置插入n个elem数据,无返回值。
insert(pos,beg,end);    //在pos位置插入[beg,end)区间的数据,无返回值。

*/

void test004()
{
	deque<int>d1;

	d1.push_back(10);
	d1.push_back(20);
	d1.push_front(100);
	d1.push_front(200);
	printDeque4(d1);

	//insert
	d1.insert(d1.begin(), 1000);
	printDeque4(d1);

	d1.insert(d1.begin(), 2, 10000);
	printDeque4(d1);

	//按照区间进行插入
	deque<int>d2;
	d2.push_back(1);
	d2.push_back(2);
	d2.push_back(3);

	d1.insert(d1.begin(), d2.begin(), d2.end());          //在d1.begin()的位置 插入d2.begin(), d2.end()区间的值
	printDeque4(d1);

}

/*
erase(beg,end);         //删除[beg,end)区间的数据,返回下一个数据的位置。
erase(pos);`            //删除pos位置的数据,返回下一个数据的位置。
clear();                //清空容器的所有数据
*/

void test0004()
{
	deque<int>d1;

	d1.push_back(10);
	d1.push_back(20);
	d1.push_front(100);
	d1.push_front(200);
	printDeque4(d1);

	//删除
	deque<int>::iterator it = d1.begin();
	it++;               //迭代器可以偏移
	d1.erase(it);
	printDeque4(d1);

	d1.erase(d1.begin(), d1.end());
	printDeque4(d1);

	//清空
	//d1.clear();
}

int main(void)
{
	test04();
	cout << "-----------------------------------" << endl;
	test004();
	cout << "-----------------------------------" << endl;
	test0004();


	system("pause");
	return 0;
}

to sum up:

  • The positions provided for insertion and deletion are iterators!

  • End plug --- push_back

  • End delete --- pop_back

  • Head plug --- push_front

  • Header delete --- pop_front

6, deque data access

Function description:

  • Access to data in deque

Function prototype:

  • at(int idx); //Return the data pointed to by the index idx

  • operator[]; //Return the data pointed to by the index idx

  • front(); //Return the first data element in the container

  • back(); //Return to the last data element in the container

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

/*
at(int idx);      //返回索引idx所指的数据
operator[];       //返回索引idx所指的数据
front();          //返回容器中第一个数据元素
back();           //返回容器中最后一个数据元素

*/

void test05()
{
	deque<int>d1;
	d1.push_back(10);
	d1.push_back(20);
	d1.push_back(30);
	d1.push_front(100);
	d1.push_front(200);
	d1.push_front(300);

	//输出
	//通过[]
	for (int i = 0; i < d1.size(); i++)
	{
		cout << d1[i] << " ";
	}
	cout << endl;

	//通过at访问
	for (int i = 0; i < d1.size(); i++)
	{
		cout << d1.at(i) << " ";
	}
	cout << endl;

	//访问头元素
	cout << "第一个元素是:" << d1.front() << endl;
	cout << "最后一个元素是:" << d1.back() << endl;
}

int main(void)
{
	test05();
	system("pause");
	return 0;
}

to sum up:

  • In addition to using iterators to get the elements in the deque container, [] and at can also be used

  • front returns the first element of the container

  • back returns the last element of the container

7, deque sort

Function description:

  • Use algorithms to sort deque containers

algorithm:

  • sort(iterator beg, iterator end) //Sort the elements in the range of beg and end

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


void printDequq6(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << (*it) << " ";
	}
	cout << endl;
}

void test06()
{
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_front(100);
	d.push_front(200);
	d.push_front(300);

	cout << "排序前:";
	//输出
	printDequq6(d);

	//排序  支持随机访问的迭代器的容器,都可以用sort算法直接进行排序
	//vector 也可以利用sort排序
	sort(d.begin(), d.end());
	
	cout << "排序后:";    
	printDequq6(d);   //默认升序
}

int main(void)
{
	test06();
	system("pause");
	return 0;
}

Summary: The sort algorithm is very practical, just include the header file algorithm when using it.

Guess you like

Origin blog.csdn.net/Zhouzi_heng/article/details/115180716