首先,vector中并没有remove成员方法,这里说的remove是STL算法中的remove函数
vector的erase和remove的区别
- 知道erase,以及存在的问题,会导致迭代器失效
- ⁉️但remove原理真的忘记了,不知道
- 查找了下,remove是std中的算法,vector中没有该方法,两个区别挺大的,因为remove只是移动,并不删除
- 具体,看一下remove的实现就明白了:【algorithm-remove】
- STL中remove()只是将待删除元素之后的元素移动到vector的前端,而不是删除。若要真正移除,需要搭配使用erase()。
- 1.、erase() causes large amount of copies while remove() just does a logical delete and leaves vector unchanged by moving element around.
- 2、If you need to remove multiple elements, remove() will copy elements only once to its final position while erase() would do this multiple times.
- 3、erase() works best with elements in a position, remove() is best while working with range of elements.
remove函数实现原理: from 【algorithm-remove】
template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator result = first;
while (first!=last) {
if (!(*first == val)) {
if (result!=first)
*result = move(*first);
++result;
}
++first;
}
return result;
}
测试代码:
#include <vector>
#include <string>
#include <iostream>
#include <typeinfo>
#include <algorithm>
using namespace std;
int main()
{
vector<int> vec;
cout << ",size = " << vec.size() << ", capacity " << vec.capacity() << endl;
vec.push_back(1); //没有push_back,下面的语句将发生段错误, 因为vec[0]的原因,上面的vec并没分配内存
cout << "vec[0] = " << vec[0] << ",size = " << vec.size() << ", capacity " << vec.capacity() << endl;
vec.push_back(2);
cout << "vec[0] = " << vec[0] << ",size = " << vec.size() << ", capacity " << vec.capacity() << endl;
vec.push_back(3);
cout << "vec[0] = " << vec[0] << ",size = " << vec.size() << ", capacity " << vec.capacity() << endl;
vec.push_back(4);
cout << "vec[0] = " << vec[0] << ",size = " << vec.size() << ", capacity " << vec.capacity() << endl;
vec.push_back(5);
vec.push_back(6);
cout << "vec[0] = " << vec[0] << ",size = " << vec.size() << ", capacity " << vec.capacity() << endl;
cout << "vec[10] = " << vec[10] << endl;
cout << vec[0] << " " << vec[10] << endl;
cout << "vec[0] = " << vec[0] << ",size = " << vec.size() << ", capacity " << vec.capacity() << endl;
cout << &vec[0] << endl;
decltype(auto) lamb = []()
{ return 5; };
cout << lamb << endl;
cout << typeid(lamb).name() << endl;
cout << typeid(vec).name() << endl;
cout << typeid(vec[0]).name() << endl;
// cout << typeid([](){return 5;}).name() << endl;
// vec.assign(5, 100);
// for (auto it : vec)
// {
// cout << it << " ";
// }
// cout << endl;
// cout << &vec[0] << endl;
// cout << "\n---------test assgin----------" << endl;
// vec.assign(10, 100);
for (auto it : vec)
{
cout << it << " ";
}
cout << endl;
cout << &vec[0] << endl;
cout << "size = " << vec.size() << ", capacity " << vec.capacity() << endl;
cout << "\n---------test remove----------" << endl;
vec.push_back(10);
std::remove(vec.begin(), vec.end(), 1);
for (auto it : vec)
{
cout << it << " ";
}
cout << endl;
cout << &vec[0] << endl;
cout << "size = " << vec.size() << ", capacity " << vec.capacity() << endl;
cout << "\n---------test clear----------" << endl;
vec.clear();
cout << "vec[0] = " << vec[0] << ",size = " << vec.size() << ", capacity " << vec.capacity() << endl;
// vec.shrink_to_fit();
vector<int>().swap(vec); //效果和shrink_to_fit类似
cout << "size = " << vec.size() << ", capacity " << vec.capacity() << endl;
vector<int> v;
//Insert values 1 to 10
v.push_back(20);
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(20);
v.push_back(10);
cout << "\n---------test remove----------" << endl;
for(int i=0;i<v.size(); i++){
cout << v[i] << " ";
}
cout << "\nsize = " << v.size() << ", capacity " << v.capacity() << endl;
vector<int>::iterator new_end;
new_end = remove(v.begin(), v.end(), 20);
for(int i=0;i<v.size(); i++){
cout << v[i] << " ";
}
cout << "\nsize = " << v.size() << ", capacity " << v.capacity() << endl;
}