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. springboot做邮件发送功能时报错No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available:的问题解决方案

    1.检查application.yml中的配置是否正确 spring.mail.host=smtp.xxx.comspring.mail.username=xxx@xxx.comspring.mail ...

  2. SQL SERVER数据库Left Join用法

    Left Join基本语法: SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的 ...

  3. php(tp5)实现分页效果

    public function admin(){ if(request()->isPost()){ //获取第二页的数据传current = 2过来即可 $post['origin'] = in ...

  4. MathType总结编辑括号的类型(下)

    在数学中,所涉及到的公式总是会有各种各样的情况,对于括号这些都是最常见的了.在最开始的四则基本运算中我们学会了使用括号,而随着学习的不断深入,所涉及到的符号与公式都越来越多,对于括号的类型也是使用得非 ...

  5. 使用Camtasia创作抖音卡点视频

    空闲的时候刷一刷抖音相信已经成为很多人的日常啦,抖音里面的视频形式多种多样,而其中的卡点视频更是被大家热烈追捧.如果你外出旅行拍摄了很多好看的照片,就很适合用卡点视频的形式展现出来. 如果你想要制作这 ...

  6. guitar pro系列教程(十三):Guitar Pro教程之打谱使用技巧

    前面我们有讲过关于{cms_selflink page='index' text='Guitar Pro'}在声音方面的一些使用技巧,Guitar Pro在打谱,试听,伴奏方面对于刚学吉他作谱的朋友们 ...

  7. React Native两种加载图片的方式

    1 加载网络图片 通过uri就可以加载网络图片 <Image source={{uri:'http://facebook.github.io/react/img/logo_og.png'}} s ...

  8. try-with-resources和multi-catch的使用

    1.首先说一下以前开发中我们在处理异常时,我们会使用try-catch-finally来处理异常. //使用try-catch-finallypublic static void main(Strin ...

  9. 【Usaco 2009 Gold】JZOJ2020年9月19日提高B组T4 过路费

    [Usaco 2009 Gold]JZOJ2020年9月19日提高B组T4 过路费 题目 Description 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生财之 ...

  10. 【GDOI2014模拟】JZOJ2020年8月14日T2 网格

    [GDOI2014模拟]JZOJ2020年8月14日T2 网格 题目 Time and Memory Limits Description 某城市的街道呈网格状,左下角坐标为A(0, 0),右上角坐标 ...