Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector和动态数组一样,插入数据和删除数据可以动态改变大小,存储空间由容器处理。

vector::clear()

clear() function is used to remove all the elements of the vector container, thus making it size 0.函数clear()用来删除vector容器的所有元素。
Syntax :

vectorname.clear()
Parameters :
No parameters are passed.
Result :
All the elements of the vector are
removed ( or destroyed )

Examples:

Input  : myvector= {1, 2, 3, 4, 5};myvector.clear();
Output : myvector= {}Input  : myvector= {};myvector.clear();
Output : myvector= {}

Errors and Exceptions

1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.

// CPP program to illustrate
// Implementation of clear() function
#include <iostream>
#include <vector>
using namespace std; int main()
{ vector<int> myvector; myvector.push_back(1); myvector.push_back(2); myvector.push_back(3); myvector.push_back(4); myvector.push_back(5); // Vector becomes 1, 2, 3, 4, 5 myvector.clear(); // vector becomes empty // Printing the vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; return 0;
}

Output:

No Output

Time Complexity: O(N)
All elements are destroyed one by one.元素是一个一个删除。

vector::erase()

erase() function is used to remove elements from a container from the specified position or range.erase()函数是删除特定元素或特定范围的元素。

Syntax :

1. vectorname.erase()
2. vectorname.erase(startingposition, endingposition)
Parameters :
Position of the element to be removed in the form of iterator. position是vector中元素的下标
or the range specified using start and end iterator.
Result :
Elements are removed from the specified
position of the container.

Examples:

Input  : myvector= {1, 2, 3, 4, 5}, iterator= 2myvector.erase(iterator);
Output : 1, 2, 4, 5Input  : myvector= {1, 2, 3, 4, 5, 6, 7, 8}, iterator1= 3, iterator2= 6myvector.erase(iterator1, iterator2);
Output : 1, 2, 3, 8

Errors and Exceptions

1. It has a no exception throw guarantee, if the position is valid.
2. Shows undefined behaviour otherwise.

Removing element from particular position

// CPP program to illustrate
// working of erase() function
#include <iostream>
#include <vector>
using namespace std; int main()
{ vector<int> myvector{ 1, 2, 3, 4, 5 }; vector<int>::iterator it; it = myvector.begin(); myvector.erase(it); // Printing the Vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; return 0;
}

Output:

2 3 4 5

Removing elements within a range

// CPP program to illustrate
// Implementation of erase() function
#include <iostream>
#include <vector>
using namespace std; int main()
{ vector<int> myvector{ 1, 2, 3, 4, 5 }; vector<int>::iterator it1, it2; it1 = myvector.begin(); it2 = myvector.end(); it2--; it2--; myvector.erase(it1, it2); // Printing the Vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; return 0;
}

Output:

4 5

Application
Given a list of integers, remove all the even elements from the vector and print the vector. 给定一系列整数,删除偶数的元素,并打印出来。

Input  :1, 2, 3, 4, 5, 6, 7, 8, 9
Output :1 3 5 7 9
Explanation - 2, 4, 6 and 8 which are even are erased from the vector

Algorithm
1. Run a loop till the size of the vector. 运行一个循环,直到vector的尾部。
2. Check if the element at each position is divisible by 2, if yes, remove the element and decrement iterator.
3. Print the final vector.

// CPP program to illustrate
// Application of erase() function
#include <iostream>
#include <vector>
using namespace std; int main()
{ vector<int> myvector{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (auto i = myvector.begin(); i != myvector.end(); ++i) { if (*i % 2 == 0) { myvector.erase(i); i--; } } // Printing the vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; return 0;
}

Output:

1 3 5 7 9

Time Complexity: O(N) in the worst case when the 1st element is deleted while O(1) in the best case when the last element is deleted. O(N)是删除第一个元素的最坏结果,O(1)是删除最后一个元素的最好的结果。

clear() vs erase(), When to use what?

clear() removes all the elements from a vector container, thus making its size 0. All the elements of the vector are removed using clear() function.删除vector容器中所有元素,大小变为0,
erase() function on the other hand, is used to remove specific elements from the container or a range of elements from the container, thus reducing its size by the number of elements removed. 删除特定位置或特定范围的元素,大小下降了删除的元素的个数。

Recommended Posts:

  • Different ways to delete elements in std::map (erase() and clear())
  • Difference between std::remove and vector::erase for vectors
  • deque::clear() and deque::erase() in C++ STL
  • vector::front() and vector::back() in C++ STL
  • vector::push_back() and vector::pop_back() in C++ STL
  • vector::empty() and vector::size() in C++ STL
  • vector::crend() & vector::crbegin() with example
  • vector :: cbegin() and vector :: cend() in C++ STL
  • set::erase in C++ STL
  • std::string::erase in C++
  • map erase() function in C++ STL
  • multiset erase() in C++ STL
  • multimap::erase() in C++ STL
  • unordered_map erase in C++ STL
  • unordered_multimap erase in C++ STL

vector erase() and clear() in C++ -- vector的函数erase()和clear()相关推荐

  1. 详解vector容器(应用+模拟实现,vector相关练习题)

    vector容器 动态的顺序表,数组. vector操作 vector操作及其概念 构造 vector<int>v1;vector<int>v2(10, 5);vector&l ...

  2. 【转】C++学习二 vector的用法(使用sort对于vector排序)

    一.vector的介绍 vector是C++里面的一个容器,也是我们数学上面理解的向量,有一些比较常见的操作. 二.vector的定义 #include<vector> using nam ...

  3. c++ vector二维数组初始化与vector切片

    0.用vector模拟二维数组 实际开发过程中,经常会使用vector嵌套的方式来模拟二维数组,下面我们详细分析一下如何对其进行初始化. 1.逐个初始化 首先我们采用逐个初始化的方式,对二维vecto ...

  4. c++ vector 一部分_C++逆向学习(二) vector

    此文为原创文章 作者:ret2nullptr@先知社区 恭喜作者获得 价值100元的天猫超市享淘卡一张 欢迎更多优质原创.翻译作者加入 ASRC文章奖励计划 欢迎多多投稿到先知社区 每天一篇优质技术好 ...

  5. vector机器人 WAYS TO INTERACT WITH VECTOR 与 VECTOR 的交互方式

    目录 Sections: 部分: Things to say to Vector 对 Vector 要说的话 Hey Vector 嘿 Vector Vector Q&A 向量问答 Sound ...

  6. vector所存储对象地址在vector操作过程中发生变化

    vector所存储对象地址在vector操作过程中发生变化 在编写搜索算法存储搜索路径的时候,以为无论如何vector的所有元素的地址都不变,比如vector<int> [0] ,就想去记 ...

  7. 如何将一个vector内容赋值给另一个vector C/C++

    在c/c++中,将一个vector内容赋值给另一个vector的方法大概有如下四个: 方法1: vector<int > v1(v2);//声明 方法2:使用函数assign进行赋值: v ...

  8. vector<int> v 与 vector<int> v(n) 的区别

    使用vector的注意事项(切记): 使用 vector<int> v; 声明一个容器v时,如果没有给他预定存储空间(如:vector<int> v;),则可以直接使用v.pu ...

  9. java Vector 过时了_为什么Java Vector(和Stack)类被认为已过时或已弃用?

    为什么Java向量被认为是遗留的类,过时的或被弃用的? 使用并发时,它的使用是否有效? 如果我不想手动同步对象,只想使用线程安全集合而不需要对底层数组进行新的复制(如CopyOnWriteArrayL ...

最新文章

  1. python读取excel部分值存入另一个excel-python3读取excel文件只提取某些行某些列的值方法...
  2. Python编程基础:第三十四节 文件移动Move a File
  3. 你所知道的都是错的!产品经理的十大认知错误
  4. DotNetNuke 4.3.5 StarterKit C#模板无法运行Bug
  5. 那些年踩过的eleUl上传图片的坑?
  6. linux配置java环境变量(转)
  7. MATLAB电路仿真搭建教程
  8. 加速pip下载:更换pip源
  9. 在html里ff3d3d是什么颜色,HTML颜色参考
  10. 【小迪安全】web安全|渗透测试|网络安全 | 学习笔记-7
  11. 联通沃云发布全新战略:强大基座,就在身边
  12. Verilog 实现占空比为3/5 2/5 1/5 50% 的五分频信号 断言SVA查看波形
  13. 运放电压和电流负反馈的讨论
  14. 【Java】如何优雅的使用HttpClient
  15. AsyncTask用法
  16. 【Unity】Firebase-Google登录身份验证功能接入流程
  17. 贝叶斯分类方法-例题
  18. saturn pcb toolkit相关功能界面介绍
  19. 免费c语言程序设计题库app,2020知到APPC语言程序设计(青岛职业技术学院)答案搜题公众号...
  20. 前端后台以及游戏中使用Google Protocol Buffer详解

热门文章

  1. 来吧,一文彻底搞懂Java中最特殊的存在——null
  2. 电脑wifi网络连接,可以上QQ和微信但是无法浏览器无法使用解决方法
  3. time模块训练:打印出某网店每天累计的购物次数
  4. Freemarker使用mht制作导出word模板
  5. 安装Cab升级文件的6个方法
  6. 【Windows】Windows10家庭版永久关闭自动更新
  7. UVA10859 placin glampposts(放置街灯)
  8. Java 导入Excel数据
  9. Go语言核心:Go的基本结构
  10. 正方教务系统新版php,正方教务系统最新sql注射漏洞修复 | CN-SEC 中文网