STL之deque使用简介
deque函数列表
|
|
构造函数
deque<Elem> c 创建一个空的deque
deque<Elem> c1(c2) 复制一个deque。
deque<Elem> c(n) 创建一个deque,含有n个数据,数据均已缺省构造产生。
deque<Elem> c(n, elem) 创建一个含有n个elem拷贝的deque。
deque<Elem> c(beg,end) 创建一个以[beg;end)区间的deque。
~deque<Elem>() 销毁所有数据,释放内存。
成员函数
c.begin()返回指向第一个元素的迭代器
c.end()返回指向最后一个元素下一个位置的迭代器
deque<int> d {,,,,};
deque<int>::iterator it;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
c.rbegin()返回指向反向队列的第一个元素的迭代器(即原队列的最后一个元素)
c.rend()返回指向反向队列的最后一个元素的下一个位置(即原队列的第一个元素的前一个位置)
deque<int> d {,,,,};
deque<int>::reverse_iterator it;
for(it=d.rbegin();it!=d.rend();it++){
cout << *it << " ";
}
cout << endl;
operator=赋值运算符重载
deque<int> d1 {,,,,},d2;
d2 = d1;
deque<int>::iterator it;
for(it=d2.begin();it!=d2.end();it++){
cout << *it << " ";
}
cout << endl;
c.assign(n,num)将n个num拷贝复制到容器c
c.assign(beg,end)将[beg,end)区间的数据拷贝复制到容器c
deque<int> d1 {,,,,},d2;
d2.assign(, );
deque<int>::iterator it;
cout << "d2.assign(n,num):";
for(it=d2.begin();it!=d2.end();it++){
cout << *it << " ";
}
d2.assign(d1.begin(), d1.begin()+);
cout << "d2.assign(beg,end):";
for(it=d2.begin();it!=d2.end();it++){
cout << *it << " ";
}
cout << endl;
c.at(pos)返回索引为pos的位置的元素,会执行边界检查,如果越界抛出out_of_range异常
deque<int> d {,,,,};
cout << "d.at(pos):" << d.at();
return ;
c.operator[]下标运算符重载
c.empty()判断c容器是否为空
c.front()返回c容器的第一个元素
c.back()返回c容器的最后一个元素
c.size()返回c容器中实际拥有的元素个数
c.max_size()返回c容器可能存放元素的最大数量
c.insert(pos,num)在pos位置插入元素num
c.insert(pos,n,num)在pos位置插入n个元素num
c.insert(pos,beg,end)在pos位置插入区间为[beg,end)的元素
deque<int> d {,,,,};
deque<int>::iterator it;
cout << "insert before:" ;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
d.insert(d.end(),);
d.insert(d.end(), ,);
int a[] = {,,,,};
d.insert(d.begin(),a,a+);
cout << "insert after:" ;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
c.erase(pos)删除pos位置的元素c.erase(beg,end)删除区间为[beg,end)的元素
c.erase(beg,end)删除区间为[beg,end)之间的元素
deque<int> d {,,,,};
d.erase(d.begin());
deque<int>::iterator it;
cout << "erase(pos) after:" ;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
d.erase(d.begin(), d.begin()+);
cout << "erase(beg,end) after:" ;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
c.push_back(num)在末尾位置插入元素
c.pop_back()删除末尾位置的元素
c.push_front(num)在开头位置插入元素
c.pop_front()删除开头位置的元素
deque<int> d {,,,,};
d.push_back();
deque<int>::iterator it;
cout << "push_back(num):" ;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
d.pop_back();
cout << "pop_back(num):" ;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
d.push_front();
cout << "push_front(num):" ;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
d.pop_front();
cout << "pop_front(num):" ;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
return ;
c.resize(num)从新定义容器的大小
deque<int> d {,,,,};
cout << "d.size():" << d.size() << endl;
d.resize(d.size()+);
cout << "d.resize() after:" << d.size() <<endl;
deque<int>::iterator it;
cout << "resize() after:" ;
for(it=d.begin();it!=d.end();it++){
cout << *it << " ";
}
cout << endl;
c1.swap(c2)交换容器c1,c2;
swap(c1,c2)同上
deque<int> d1 {,,,,},d2,d3;
d1.swap(d2);
deque<int>::iterator it;
cout << "d1 swap after:" ;
for(it=d1.begin();it!=d1.end();it++){
cout << *it << " ";
}
cout << endl;
cout << "d2 swap after:" ;
for(it=d2.begin();it!=d2.end();it++){
cout << *it << " ";
}
cout << endl;
swap(d3,d2);
cout << "d3 swap after:" ;
for(it=d3.begin();it!=d3.end();it++){
cout << *it << " ";
}
cout << endl;
重载运算符
operator==
operator!=
operator<
operator<=
operator>
operator>=
STL之deque使用简介的更多相关文章
- 带你深入理解STL之Deque容器
在介绍STL的deque的容器之前,我们先来总结一下vector和list的优缺点.vector在内存中是分配一段连续的内存空间进行存储,其迭代器采用原生指针即可,因此其支持随机访问和存储,支持下标操 ...
- STL之deque双向队列
deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,提供随机访问,deque在接口上和vector非常相似,下面列出deque的常用成员函数: Table 6.9. C ...
- STL中deque
以下学习一下STL中另一种序列容器——deque. deque表示double-ended queue,即双向队列,deque是通过作为动态数组的方式实现的,这样可以在两端插入元素.因此,deque可 ...
- STL之Deque容器
1.Deque容器 1)deque是“double-ended queue”的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的. 2)deque在接口上和vect ...
- STL之容器(containers) 简介
什么是容器? 容器用来存储数据的,数据可以是用户自定义类型(对象),也可以是预定义类型,c++中的容器主要使用如vector,list (顺序容器) 这些都是已经封装好了. 1.结构(struct): ...
- STL中deque 解析
一.deque的中控器 deque是连续空间(至少逻辑上看来如此),连续线性空间总令我们联想到array或vector.array无法成长,vector虽可成长,却只能向尾端成长,而且其所谓的成长原是 ...
- [转]STL之deque容器详解
Deque 容器 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中 ...
- STL之Deque的使用方法
STL 中类 stack 实现了一个栈 1)push 能够插入元素 2)pop 移除栈顶元素 使用的时候,需要包含头文件 #include <stack>,stack 被声明如下: nam ...
- STL之deque
deque是一种优化了的,对序列两段进行添加和删除操作的基本序列容器.它允许较为快速的随机访问,但它不像vector把所有对象保存在一块连续的内存块,而是采用多个连续的存储块.向deque两段添加或删 ...
随机推荐
- Mybatis之批量更新操作
更新单条记录 UPDATE course SET name = 'course1' WHERE id = 'id1'; 更新多条记录的同一个字段为同一个值 UPDATE course SET name ...
- linux awk 内置函数详细介绍(实例)
这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y ...
- 根据值设置select的选中项
$('.selector').attr("checked", true); <s:iterator value="jobSelect" id=" ...
- Eclipse使用的小技巧
1.在右键new菜单栏中添加新建JSP文件 window->perspective->customize perspective->shortcuts->web->把JS ...
- v-for的显示过滤/排序结果
对于v-for列表渲染指令,项目中很常用的额,但是我们一般可能在从后端接口拿到数据的时候就把数据通过循环整理改造成自己想要的样子了.有时候可能对于不同的列表需求,还要在data里多造一份数据. 这种做 ...
- this以及执行上下文概念的重新认识
在理解this的绑定过程之前,必须要先明白调用位置,调用位置指的是函数在代码中被调用的位置,而不是声明所在的位置. (ES6的箭头函数不在该范围内,它的this在声明时已经绑定了,而不是取决于调用时. ...
- CentOS7部署LAMP+xcache (php-fpm模式)
此次实验准备3台CentOS7服务器,版本号:CentOS Linux release 7.2.1511. 搭建Apache服务器 通过 yum -y install httpd 安装Apache: ...
- linux select用法
select 是linux i/o 复用技术之一 man 2 select #include <sys/select.h> /* According to earlier standard ...
- 裸机——Nand
1.首先需要知道Nand的基础知识 从Nand的芯片手册可以获得 我使用的芯片手册是 K9F2G08 首先从芯片手册的名称可以获得信息: K9F:三星 2G : 2Gb (256MB) 08 ...
- [Bzoj4289]PA2012 Tax(Dijkstra+技巧建图)
Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...