STL——容器(deque)deque 的删除 clear() erase()
deque.clear();
//移除容器的所有数据
1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 deque<int> deqInt_A(num, num + size(num));
10
11 cout << "deqInt_A中的元素个数为:";
12 cout << deqInt_A.size() << endl;
13
14 cout << "deqInt_A所占用内存:";
15 cout << sizeof(deqInt_A) << endl;
16
17 cout << "遍历deqInt_A:";
18 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
19 {
20 cout << *it << " ";
21 }
22
23 //删除容器中
24 deqInt_A.clear();
25
26 cout << "\n\nclear后,deqInt_A中的元素个数为:";
27 cout << deqInt_A.size();
28 cout << "\ndeqInt_A所占用内存:";
29 cout << sizeof(deqInt_A);
30 cout << "\nclear后,遍历deqInt_A:" << endl;
31 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
32 {
33 cout << *it << " ";
34 }
35
36 return 0;
37 }
打印结果:
可以发现内存是并没有释放的

deque.erase(beg,end);
//删除[beg,end)区间的数据,返回下一个数据的位置。
1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 deque<int> deqInt_A(num, num + size(num));
10
11 cout << "遍历deqInt_A:";
12 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
13 {
14 cout << *it << " ";
15 }
16 cout << "\ndeqInt_A中的元素个数为:";
17 cout << deqInt_A.size() << endl;
18
19 cout << "deqInt_A所占用内存:";
20 cout << sizeof(deqInt_A) << endl;
21
22
23
24 cout << "\n删除容器中前三个元素后,遍历deqInt_A:";
25 //删除容器中前三个元素后,用返回的迭代器遍历,返回了下一个元素的位置
26 for (deque<int>::iterator it = deqInt_A.erase(deqInt_A.begin(), deqInt_A.begin() +3); it != deqInt_A.end(); it++)
27 {
28 cout << *it << " ";
29 }
30 cout << "\n删除容器中前三个元素后,deqInt_A中的元素个数为:";
31 cout << deqInt_A.size();
32
33 cout << "\n删除容器中前三个元素后,deqInt_A所占用内存:";
34 cout << sizeof(deqInt_A);
35
36 return 0;
37 }
打印结果:
可以发现,deqInt_A中的元素删除后,占用的内存空间大小并没有变化

deque.erase(pos);
//删除pos位置的数据,返回下一个数据的位置。
1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 deque<int> deqInt_A(num, num + size(num));
10
11 cout << "遍历deqInt_A:";
12 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
13 {
14 cout << *it << " ";
15 }
16 cout << "\ndeqInt_A中的元素个数为:";
17 cout << deqInt_A.size() << endl;
18
19 cout << "deqInt_A所占用内存:";
20 cout << sizeof(deqInt_A) << endl;
21
22 cout << "\n删除容器中第三个元素后边的元素后,用返回的迭代器遍历后边的元素:";
23 //删除容器中第三个元素后边的元素后,用返回的迭代器遍历,返回了下一个元素的位置,注意这个数字不是第三个,是第三个之后的那个元素
24 for (deque<int>::iterator it = deqInt_A.erase(deqInt_A.begin() + 3); it != deqInt_A.end(); it++)
25 {
26 cout << *it << " ";
27 }
28 cout << "\n遍历 deqInt_A 中所有的元素:";
29 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
30 {
31 cout << *it << " ";
32 }
33 cout << "\n删除容器中第三个元素后边的元素后,deqInt_A中的元素个数为:";
34 cout << deqInt_A.size();
35
36 cout << "\n删除容器中第三个元素后边的元素后,deqInt_A所占用内存:";
37 cout << sizeof(deqInt_A);
38
39 return 0;
40 }
打印结果:

一般在项目中删除单个元素会这样用:
1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 deque<int> deqInt_A(num, num + size(num));
10
11 //删除等于 444 的元素
12 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end();)
13 {
14 if (*it == 444)
15 {
16 it = deqInt_A.erase(it); //删除元素后,erase 会返回下一个元素的位置,相当于 it++操作了
17 }
18 cout << *it << " ";
19 it++; //不把it++ 写到for循环的条件语句中,是为了避免删除元素后的越界访问,比如删除 444 后,555会在444的位置,这时候it++就会越界
20 }
21
22 return 0;
23 }
打印结果:

=======================================================================================================================
STL——容器(deque)deque 的删除 clear() erase()的更多相关文章
- STL——容器(deque) 构造 & 头尾添加删除元素
1.deque容器概念 deque是"double-ended queue"的缩写,和vector一样都是STL的容器,唯一不同的是:deque是双端数组,而vector是单端的. ...
- STL容器:deque双端队列学习
所谓deque,是"double-ended queue"的缩写; 它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速; 在中间插入元素比较费时,因为需要移动其它元 ...
- STL容器分析--deque
deque,故名思义,双向队列.可以在头尾进行插入删除. 而STL中采用了链表+线性表的数据结构来实现deque,因而除了满足双向队列的特点以外,还支持随机访问. 下面,贴一段代码. 总览:双向队列是 ...
- STL——容器(deque) deque 的大小
1. deque 的大小 deque.size(); //返回容器中元素的个数 1 #include <iostream> 2 #include <dequ ...
- STL——容器(deque) 元素的存取&迭代器
1. deque 的数据存取 这个部分和 vector 几乎一样 第一 使用下标操作 dequeName[0] = 100; //小心越界 第二 使用at 方法 如: dequeName.at(2 ...
- STL——容器(deque)deque 的插入 insert()
deque.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置. 1 #include <iostream> 2 #include <d ...
- STL——容器(deque) deque 的赋值 assign() operator=() swap()
deque 的赋值分下边4种方法: deque.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身.注意该区间是左闭右开的区间. 1 #include <io ...
- ACM常用STL容器
// STL(标准模板库),由三大部分组成:容器,算法,迭代器 // STL六大组件:container(容器),algorthm(算法),iterator(迭代器) // function obje ...
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
序列性容器::(vector和list和deque) erase迭代器不仅使所有指向被删元素的迭代器失效,而且使被 删元素之后的所有迭代器失效,所以不能使用erase(iter++)的方 式, ...
随机推荐
- Centos7升级内核后无法启动解决办法
前言 这个问题存在有一段时间了,之前做的centos7的ISO,在进行内核的升级以后就存在这个问题: 系统盘在板载sata口上是可以正常启动新内核并且能识别面板硬盘 系统盘插在面板口上新内核无法启动, ...
- Vue-router插件使用
单页面原理 Vue是单页面开发,即页面不刷新. 页面不刷新,而又要根据用户选择完成内容的更新该怎么做?Vue中采用锚点来完成. 如访问http://127.0.0.1#/index就是主页,而访问ht ...
- python3 多线程批量验证POC模板
#coding:utf-8 import threading,Queue,sys,os class RedisUN(threading.Thread): def __init__(self,queue ...
- HTML5 localStorageXSS漏洞
localStorage基础 Window localStorage 属性 HTML5 提供了两种新的本地存储方案,sessionStorage和localStorage,统称WebStorage. ...
- bWAPP----Mail Header Injection (SMTP)
Mail Header Injection (SMTP) 本地没有搭环境,没法演示,附上转载的 https://www.acunetix.com/blog/articles/email-header- ...
- __FUNCTION__
- 上周我面了个三年 Javaer,这几个问题都没答出来
身为 Java Web 开发我发现很多人一些 Web 基础问题都答不上来. 上周我面试了一个三年经验的小伙子,一开始我问他 HTTP/1.HTTP/2相关的他到是能答点东西出来. 后来我问他:你知道 ...
- 面试官:小伙子,你给我说一下你对MySQL索引的理解吧
一.索引是什么? 索引是帮助MySQL高效获取数据的数据结构. 二.索引能干什么? 索引非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要.索引能够轻易将查询性能提高好几个数量级,总 ...
- Mac 安装Homebrew慢的问题解决
一开始安装,在官网上的命令: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...
- 通过Consul Raft库打造自己的分布式系统
通用的CP系统有etcd和consul, 通用的对立面就是专用系统. 所以在某些场合是有这种需求的. 然而etcd embed的可用性极差, Windows上面跑会出现各种问题, 而且不能定制协议, ...