deque容器的用法

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

1、deque的概述

deque容器概念
deque是“double-ended queue"的缩写,和vector一样都是STL的容器,唯一不同的是:
deque是双端数组,而vector是单端的。

在这里插入图片描述
Deque特点:
deque在接口上和 vector非常相似,在许多操作的地方可以直接替换。
deque 可以随机存取元素(支持索引值直接存取,用[]操作符或at()方法。
deque头部和尾部添加或移除元素都非常快速,但是在中部安插元素或移除元素比较费
时。
使用时,包含头文件:#include

2、deque 对象的默认构造

(1)deque也是采用模板类实现。
deque对象的默认构造形式: deque deqT
代码如下:

deque <int> deqInt;     //存放inti的 deque容器。
deque <float> deqFloat; //存放float 的deque 容器。
deque <student> deqStu; //存放studen 的 deque容器。

注点。尖括号内还可以设置指针类型或自定义类型。

(2)dque带参的构造

   //方式一:给定区间
	deque<int> deqIntB(deqIntA.begin(), deqIntA.end()); //构造函数将[begend)区间中的元素拷贝给本身。
	//方式二:指定n个元素进行拷贝
	deque<int>deIntC(10, 0);
	//方式三:拷贝构造
	deque<int>deqIntD(deIntC);

(3) deque 头部和末尾的添加移除操作

deque.push_back(); //容器尾部添加一个数据
deque.push_front();//容器头部插入一个数据
deque.pop_back();  //删除容器最后一个数据
deque.pop_front(); //删除容器第一个数据

3、deque的元素的存取和迭代器

第一使用下标操作deqIntA[0]
第二使用at方法如: deqIntA.at(2)
第三接口返回的引用deqIntA.front()和deqIntA.back()

注意:第一和第二种方式必须注意越界

int main()
{
	deque<int> deqIntA;     //存放int的deque容器。

	deqIntA.push_back(1);
	deqIntA.push_back(2);
	deqIntA.push_back(3);
	deqIntA.push_back(4);
	deqIntA.push_back(5);

	int i1 = deqIntA.at(0);  //i1 = 1
	int i2 = deqIntA[1];     //i2 = 2

	
	deqIntA.at(0) = 666;    //第一个元素改成666 I
	deqIntA[1] = 888;

	deqIntA[5] = 999;       //越界访问

	int  iFront = deqIntA.front();   //666
	int iBack = deqIntA.back();      //5   

	iFront++;
	iBack++;

	deqIntA.front() = iFront;       //667
	deqIntA.back() = iBack;         //6

	cout << "deqIntA中的元素:" << endl;
	for (unsigned int i = 0; i < deqIntA.size(); i++)
	{
		cout << deqIntA[i] << endl;
	}
}

迭代器如下:

	//普通迭代器
	for (deque<int>::iterator it = deqIntA.begin(); it != deqIntA.end(); it++)
	{
		cout << *it;
		cout << " ";
	}


  //常量迭代器
	deque<int>::const_iterator cit = deqIntA.cbegin();
	for (; cit != deqIntA.cend(); cit++)
	{
		cout << *cit;
		cout << " ";
	}

	//逆转迭代器
	for (deque<int>::reverse_iterator rit = deqIntA.rbegin();
		rit != deqIntA.rend(); rit++)
	{
		cout << *rit;
		cout << " ";
	}
	

4.deque赋值和大小

deque 的赋值

deque.assign(beg,end);//将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。
deque.assign(n,elem);//将 n个elem拷贝赋值给本身。
deque& opprator=(const deque &deq);//重载等引澡作符
deque.swap(deq); //将deque 与本身的元素互换

deque的大小

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

例子:

int main()
{
	deque<int> deqIntA, deqIntB, deqIntC, deqIntD;

	deqIntA.push_back(1);
	deqIntA.push_back(2);
	deqIntA.push_back(3);
	deqIntA.push_back(4);
	deqIntA.push_back(5);
   
   //deque的赋值
	deqIntB.assign(deqIntA.begin(), deqIntA.end());
	deqIntC.assign(4, 888);
	deqIntD = deqIntA;

	
	for (deque<int>::iterator it = deqIntC.begin(); it != deqIntC.end(); ++it)
	{
		cout << *it;
		cout << " ";
	}

   //deque的大小
	int size = deqIntA.size();// 5

	deqIntA.resize(7);        // 1 2 3 4 5 0 0
	 
	deqIntA.resize(3);        //1 2 3

	deqIntA.resize(8, 1);     // 1 2 3 4 5 0 0 1

	for (deque<int>::iterator it = deqIntA.begin(); it != deqIntA.end(); ++it)
	{
		cout << *it;
		cout << " ";
	}
	return 0;
}

5、deque的插入和删除

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

int main(void)
{
  deque<int> deqIntA;
  deque<int> deqIntB;
  deqIntA.push_back(1);
  deqIntA.push_back(2) ;
  deqIntA.push_back(3);
  deqIntA.push_back(4) ;

  deqIntB.push_back(11) ;
  deqIntB.push_back(12);
  deqIntB.push_back(13) ;
  deqIntB.push_back(14) ;
  deqIntA.insert(deqIntA.begin(),0);     //0 1 2 3 4
  deqIntA.insert(deqIntA.begin()+1288);    //0,88,88,1,2,3,4
  deqIntA.insert(deqIntA.begin(), deqIntB.rbegin(),deqIntB.rend());  //14 13 12 11 0 1 2 3 4  

  //deque的删除
  //方式一单独使用擦除的接口
  deqIntA.erase(deqIntA.begin()+1);  //干掉第二个元素{1,3,4,5}
  deqIntA.erase(deqIntA. begin()+1,deqIntA.begin() +3);  //干掉3和4 ,{1,5}
  deqIntA.clear() ;  //干掉所有的元素
 for (deque<int> ::iterator it = deqIntA.begin(); it!=deqIntA.end (); ++it)
  {
    cout<<*it;
    cout<<" ";
  }

  //方式二使用迭代器遍历删除
   for(deque<int>: :iterator it = deqIntA.begin() ; it!=deqIntA.end()){
   if(*it ==4) {
 it = deqIntA.erase(it) ;
  }
  else
  {
  cout<<*it ;
  cout<<"”;
  it++;
  }  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值