[容器]STL之deque容器详解

来源:互联网 发布:苹果看书软件 编辑:程序博客网 时间:2024/06/15 18:14

原文地址:http://www.cnblogs.com/scandy-yuan/archive/2013/01/09/2853603.html

Deque 容器

deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容。deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中某一位置上的操作所花费的是线性时间。与vector不同的是,deque还支持从开始端插入数据:push_front()。

使用deque容器之前必须加上<deque>头文件:#include<deuqe>;

       deque属于std命名域的内容,因此需要通过命名限定:using std::deque;也可以直接使用全局的命名空间方式:using namespace std;

 

构造函数

  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()返回指向最后一个元素下一个位置的迭代器

1     deque<int> d {1,2,3,4,5};2     deque<int>::iterator it;3     for(it=d.begin();it!=d.end();it++){4         cout << *it << " ";5     }6     cout << endl;

c.rbegin()返回指向反向队列的第一个元素的迭代器(即原队列的最后一个元素)

c.rend()返回指向反向队列的最后一个元素的下一个位置(即原队列的第一个元素的前一个位置)

1     deque<int> d {1,2,3,4,5};2     deque<int>::reverse_iterator it;3     for(it=d.rbegin();it!=d.rend();it++){4         cout << *it << " ";5     }6     cout << endl;

operator=赋值运算符重载

复制代码
1     deque<int> d1 {1,2,3,4,5},d2;2     d2 = d1;3     deque<int>::iterator it;4     for(it=d2.begin();it!=d2.end();it++){5         cout << *it << " ";6     }7     cout << endl;
复制代码

c.assign(n,num)将n个num拷贝复制到容器c

c.assign(beg,end)将[beg,end)区间的数据拷贝复制到容器c

复制代码
 1     deque<int> d1 {1,2,3,4,5},d2; 2     d2.assign(2, 8); 3     deque<int>::iterator it; 4     cout << "d2.assign(n,num):"; 5     for(it=d2.begin();it!=d2.end();it++){ 6         cout << *it << " "; 7     } 8     d2.assign(d1.begin(), d1.begin()+3); 9     cout << "d2.assign(beg,end):";10     for(it=d2.begin();it!=d2.end();it++){11         cout << *it << " ";12     }13     cout << endl;
复制代码

c.at(pos)返回索引为pos的位置的元素,会执行边界检查,如果越界抛出out_of_range异常

1     deque<int> d {1,2,3,4,5};2     cout << "d.at(pos):" << d.at(2);3     return 0;

c.operator[]下标运算符重载

1     deque<int> d {1,2,3,4,5};2     cout << "d[2]:" << d[2];3     return 0;

c.empty()判断c容器是否为空

复制代码
1     deque<int> d {1,2,3,4,5};2     if(!d.empty()){3         cout << "d is not empty!" << endl;4     }else{5         cout << "d is empty!" << endl;6     }7     return 0;
复制代码

c.front()返回c容器的第一个元素

c.back()返回c容器的最后一个元素

1     deque<int> d {1,2,3,4,5};2     if(!d.empty()){3         cout << "d.front():" << d.front() << endl;4         cout << "d.back(): " << d.back() << endl;5     }

c.size()返回c容器中实际拥有的元素个数

1     deque<int> d {1,2,3,4,5};2     cout << "d.size():" << d.size() << endl;3     return 0;

c.max_size()返回c容器可能存放元素的最大数量

1     deque<int> d {1,2,3,4,5};2     cout << "d.max_size():" << d.max_size() << endl;3     return 0;

c.clear()清除c容器中拥有的所有元素

复制代码
 1     deque<int> d {1,2,3,4,5}; 2     deque<int>::iterator it; 3     cout << "clear before:" ; 4     for(it=d.begin();it!=d.end();it++){ 5         cout << *it << " "; 6     } 7     cout << endl; 8     d.clear(); 9     cout << "clear after:" ;10     for(it=d.begin();it!=d.end();it++){11         cout << *it << " ";12     }13     cout << endl;
复制代码

c.insert(pos,num)在pos位置插入元素num

c.insert(pos,n,num)在pos位置插入n个元素num

c.insert(pos,beg,end)在pos位置插入区间为[beg,end)的元素

复制代码
 1     deque<int> d {1,2,3,4,5}; 2     deque<int>::iterator it; 3     cout << "insert before:" ; 4     for(it=d.begin();it!=d.end();it++){ 5         cout << *it << " "; 6     } 7     cout << endl; 8     d.insert(d.end(),22); 9     d.insert(d.end(), 3,88);10     int a[5] = {1,2,3,4,5};11     d.insert(d.begin(),a,a+3);12     cout << "insert after:" ;13     for(it=d.begin();it!=d.end();it++){14         cout << *it << " ";15     }16     cout << endl;
复制代码

c.erase(pos)删除pos位置的元素c.erase(beg,end)删除区间为[beg,end)的元素

c.erase(beg,end)删除区间为[beg,end)之间的元素

复制代码
 1     deque<int> d {1,2,3,4,5}; 2     d.erase(d.begin()); 3     deque<int>::iterator it; 4     cout << "erase(pos) after:" ; 5     for(it=d.begin();it!=d.end();it++){ 6         cout << *it << " "; 7     } 8     cout << endl; 9     d.erase(d.begin(), d.begin()+3);10     cout << "erase(beg,end) after:" ;11     for(it=d.begin();it!=d.end();it++){12         cout << *it << " ";13     }14     cout << endl;
复制代码

c.push_back(num)在末尾位置插入元素

c.pop_back()删除末尾位置的元素

c.push_front(num)在开头位置插入元素

c.pop_front()删除开头位置的元素

复制代码
 1     deque<int> d {1,2,3,4,5}; 2     d.push_back(10); 3     deque<int>::iterator it; 4     cout << "push_back(num):" ; 5     for(it=d.begin();it!=d.end();it++){ 6             cout << *it << " "; 7     } 8     cout << endl; 9     10     d.pop_back();11     cout << "pop_back(num):" ;12     for(it=d.begin();it!=d.end();it++){13         cout << *it << " ";14     }15     cout << endl;16     17     d.push_front(10);18     cout << "push_front(num):" ;19     for(it=d.begin();it!=d.end();it++){20         cout << *it << " ";21     }22     cout << endl;23     24     d.pop_front();25     cout << "pop_front(num):" ;26     for(it=d.begin();it!=d.end();it++){27         cout << *it << " ";28     }29     cout << endl;30     return 0;
复制代码

 

c.resize(num)从新定义容器的大小

复制代码
 1     deque<int> d {1,2,3,4,5}; 2     cout << "d.size():" << d.size() << endl; 3     d.resize(d.size()+5); 4     cout << "d.resize() after:" << d.size() <<endl; 5     deque<int>::iterator it; 6     cout << "resize() after:" ; 7     for(it=d.begin();it!=d.end();it++){ 8     cout << *it << " "; 9     }10     cout << endl;
复制代码

c1.swap(c2)交换容器c1,c2;

swap(c1,c2)同上。

复制代码
 1     deque<int> d1 {1,2,3,4,5},d2,d3; 2     d1.swap(d2); 3     deque<int>::iterator it; 4     cout << "d1 swap after:" ; 5     for(it=d1.begin();it!=d1.end();it++){ 6         cout << *it << " "; 7     } 8     cout << endl; 9     cout << "d2 swap after:" ;10     for(it=d2.begin();it!=d2.end();it++){11         cout << *it << " ";12     }13     cout << endl;14     15     swap(d3,d2);16     cout << "d3 swap after:" ;17     for(it=d3.begin();it!=d3.end();it++){18         cout << *it << " ";19     }20     cout << endl;
复制代码

 

 

重载运算符

operator==

operator!=

operator<

operator<=

operator>

operator>=

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电脑无网络访问怎么办 xp连不上网怎么办 xp系统本地连接不见了怎么办 电脑xp系统本地连接怎么办 xp系统本地连接失败怎么办 xp系统找不到本地连接怎么办 台式电脑连不上网络怎么办 win7局域网要密码怎么办 xp网络不能上网怎么办 台式电脑连宽带怎么办 win7宽带813错误怎么办 电脑不显示本地连接怎么办 w7宽带连接不了怎么办 网络接收器坏了怎么办 电脑网页打开慢怎么办 win10无法添加打印机怎么办 2018杭州禁摩怎么办 中山个人怎么办社保卡 昆山房产证丢了怎么办 于一机交宽带费怎么办 租房子的怎么办宽带 乐才注册不了怎么办 分期乐登录不了怎么办 股票忘记交易密码怎么办 信用卡交易密码忘记怎么办 乐购超市怎么办会员 手机分期付款0首付怎么办 首付手机违约了怎么办 买了假东西怎么办 pk10滚雪球挂了怎么办 好彩投彩票闪退怎么办 yeezy买小了怎么办 yeezy斑马变黄了怎么办 yeezy买大了怎么办 聚宝商城被骗怎么办 电瓶车碟刹抱死怎么办 电动车分期不还怎么办 车壳子凹进去怎么办 电车电瓶不耐用怎么办 电瓶车车胎破了怎么办 电瓶车钥匙没电怎么办