1. clear() 将整个 vector 都删除

使用 vectorname.clear() 可以将整个vector 中的元素全部删除,但是内存不会释放,如下代码:

 1 #include <iostream>
2 #include <vector>
3
4 using namespace std;
5
6 int main()
7 {
8 vector<int>num(5, 111);
9 cout << "=== 使用 clear() 删除前===" << endl;
10 cout << "num 的元素个数:" << num.size() << endl;
11 cout << "num 容器的大小:" << num.capacity() << endl << endl;
12
13 cout << "=== 使用 clear() 删除后===" << endl;
14 num.clear();
15 cout << "num 的元素个数:" << num.size() << endl;
16 cout << "num 容器的大小:" << num.capacity() << endl;
17
18 return 0;
19 }

打印结果:

2. 使用 erase() 删除 单个&多个 元素

使用 vectorname.clear() 可以删除容器中的单个&多个元素,他返回的是一个迭代器,是删除之后的后一个元素的地址。

删除单个元素:

 1 #include <iostream>
2 #include <vector>
3
4 using namespace std;
5
6 int main()
7 {
8 int test[] = { 111,222,333,444,555,666 };
9 vector<int>num(test, test + 6);
10 cout << "num 的元素个数:" << num.size() << endl;
11 cout << "num 容器的大小:" << num.capacity() << endl;
12
13 cout << "=== 从头到尾遍历容器 ===" << endl;
14 for (int i = 0; i < num.size(); i++)
15 {
16 cout << num[i] << endl;
17 }
18 vector<int>::iterator it = num.erase(num.begin() + 3); //删除首地址后的第三个元素,并将后一个元素的地址返回
19
20 cout << "num 的元素个数:" << num.size() << endl;
21 cout << "num 容器的大小:" << num.capacity() << endl;
22
23 cout << endl << "=== 使用一个迭代器类型的 it 遍历容器 ===" << endl;
24
25 for (int i = 0; i < num.size() - 3; i++)
26 {
27 cout << *it++ << endl;
28 }
29
30 return 0;
31 }

打印结果:

删除多个元素:

 1 #include <iostream>
2 #include <vector>
3
4 using namespace std;
5
6 int main()
7 {
8 int test[] = { 111,222,333,444,555,666 };
9 vector<int>num(test, test + 6);
10 cout << "num 的元素个数:" << num.size() << endl;
11 cout << "num 容器的大小:" << num.capacity() << endl;
12
13 cout << "=== 从头到尾遍历容器 ===" << endl;
14 for (int i = 0; i < num.size(); i++)
15 {
16 cout << num[i] << endl;
17 }
18
19 vector<int>::iterator it = num.erase(num.begin(), num.begin() + 3); //删除1-3的元素,并将后一个元素的地址返回
20 cout << "num 的元素个数:" << num.size() << endl;
21 cout << "num 容器的大小:" << num.capacity() << endl;
22
23 cout << endl << "=== 使用一个迭代器类型的 it 遍历容器 ===" << endl;
24
25 for (int i = 0; i < num.size(); i++)
26 {
27 cout << *it++ << endl;
28 }
29
30 return 0;
31 }

打印结果:

==========================================================================================================================

STL—— 容器(vector)元素的删除的更多相关文章

  1. 从零开始写STL—容器—vector

    从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...

  2. 怎么删除STL容器的元素

    在STL容器有顺序容器和关联容器两种. 顺序容器删除元素的方法有两种: 1.c.erase(p) 从c中删除迭代器p指定的元素.p必须指向c中一个真实元素,不能等于c.end().返回一个指向p之后元 ...

  3. STL容器vector应用注意事项

    [1]提前分配足够空间以免不必要的重新分配和复制代价 关于vector容器重新分配和复制及析构释放的代价,请参见随笔<STL容器之vector>. 应用示例对比代码如下: #include ...

  4. STL容器 -- Vector

    核心:Vector 是 STL 里的一个向量容器,可以像数组那样进行随机访问,能在尾部插入元素,对于元素的删除和插入可以动态管理内存. 头文件: #include <vector> 构造函 ...

  5. vector元素的删除 remove的使用 unique的使用

    在vector删除指定元素可用以下语句 : v.erase(remove(v.begin(), v.end(), element), installed.end()); 可将vector中所有值为el ...

  6. [C++]STL容器Vector的内存释放

    直接抛出两句话,说明到底应该如何释放Vector占用的内存. “vector的clear不影响capacity,你应该swap一个空的vector.” <Effective STL>中的“ ...

  7. STL容器 vector,list,deque 性能比较

    C++的STL模板库中提供了3种容器类:vector,list,deque对于这三种容器,在觉得好用的同时,经常会让我们困惑应该选择哪一种来实现我们的逻辑.在少量数据操作的程序中随便哪一种用起来感觉差 ...

  8. STL - 容器 - vector简单应用

    VectorTest.cpp #include <vector> #include <iostream> #include <string> #include &l ...

  9. STL容器迭代器失效问题讨论

    STL源码剖析---迭代器失效小结 vector迭代器的几种失效的情况: .当插入(push_back)一个元素后,end操作返回的迭代器肯定失效. .当插入(push_back)一个元素后,capa ...

  10. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

随机推荐

  1. linux中使用head,tail,grep, sed,awk三种方法显示文档中间若干行(指定任意行)

    需要显示文本中间20-25行. 创建一个30行的文档,命名为30.txt并显示在屏幕 [root@v2-ui data]# seq 30 > 30.txt && cat 30.t ...

  2. tp5 删除图片以及文件

    控制器调用 /** * [delimg 删除单张图片] * @return [type] [description] */ public function delimg(){ if (request( ...

  3. tp5 跨域问题

    只需要三行代码,写到入口文件public/index.php处即可解决 header("Access-Control-Allow-Origin:*"); header(" ...

  4. 深度分析:Java 静态方法/变量,非静态方法/变量的区别,今天一并帮你解决!

    静态/非静态 方法/变量的写法 大家应该都明白静态方法/字段比普通方法/字段的写法要多一个static关键字,简单写下他们的写法吧,了解的可以直接略过 class Test{ // 静态变量 publ ...

  5. ThreadLocal以及强软弱虚引用

    1.ThreadLocal ThreadLocal即线程本地,可以实现每个线程存入取出TreadLocal值互不影响.因为TheadLocal底层是用了一个Map结构存放数据,而这个Map是从当前这个 ...

  6. ResNet模型

    ReeNet论文地址:Deep Residual Learning for Image Recognition Resnet的两种不同结构 上图左边的结构主要是针对深度较少的网络,当深度较大时则用右边 ...

  7. 01-01.单一职责原则(Single Responsibility)

    1.基本介绍 对于类来说的,就是一个类,应该只负责一项职责(一个类只管一件事). 如类A负责两个不同职责:职责1,职责2. 当职责1需求变更而改变A时,可能造成职责2执行错误,所以需要将类A的粒度分解 ...

  8. linux命令 ——netstat

    作用: 能查到与客户端链接状态和数量 netstat各选项参数说明: -a : 列出所有连接,服务监听,Socket信息 -c : 持续列出网络状态 #每隔一秒输出网络信息 -t : 显示TCP端口  ...

  9. 用Python爬取英雄联盟(lol)全部皮肤

    小三:"怎么了小二?一副无精打采的样子!" 小二:"唉!别提了,还不是最近又接触了一个叫英雄联盟的游戏,游戏中很多皮肤都需要花钱买,但是我钱不够呀..." 小三 ...

  10. V-指令,细节补充

    v-for遍历对象的时候 v-for=" (item) in person" 这里item是person对象里面的value值 而v-for=" (item,key) i ...