擦除/移除std::vector元素
首先要知道的是 std::remove
并不会将你想要移除的元素移出容器,他只是将你想要删除的元素用后方元素覆盖。
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> demo{1, 3, 3, 4, 3, 5};
// 覆盖要删除的元素, remove 后元素应该如右所示 1 4 5 4 3 5
auto iter = std::remove(demo.begin(), demo.end(), 3);
for (auto first = demo.begin(); first < demo.end(); ++first)
{
cout << *first << " ";
}
cout << endl;
cout << "size is :" << demo.size() << endl;
cout << "capacity is :" << demo.capacity() << endl;
return 0;
}
// 1 4 5 4 3 5
// size is :6
// capacity is :6
类似于 LeetCode 里的这题 27. 移除元素
class Solution
{
public:
int removeElement(vector<int> &nums, int val)
{
int fast = 0, slow = 0;
while (fast < nums.size())
{
if (nums[fast] == val)
{
fast++;
continue;
}
nums[slow++] = nums[fast++];
}
return slow;
}
};
然后他也类似与这题,会返回最后的慢指针作为新的 end 迭代器,我们可以用这个迭代器进行遍历
for (auto first = demo.begin(); first < iter; ++first)
{
cout << *first << " ";
}
但是那些本该被移除的元素仍在容器里,若想真正移除他们,可配合 erase
函数
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> demo{1, 3, 3, 4, 3, 5};
auto iter = std::remove(demo.begin(), demo.end(), 3);
demo.erase(iter, std::end(demo));
for (auto first = demo.begin(); first < demo.end(); ++first)
{
cout << *first << " ";
}
cout << endl;
// 输出剩余的元素
cout << "size is :" << demo.size() << endl;
cout << "capacity is :" << demo.capacity() << endl;
return 0;
}
// 1 4 5
// size is :3
// capacity is :6
这样之后 size 是变小了,但空间还存在,可以用shrink_to_fit
进行回收
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> demo{1, 3, 3, 4, 3, 5};
// 覆盖要删除的元素, remove 后元素应该如右所示1 4 5 4 3 5
auto iter = std::remove(demo.begin(), demo.end(), 3);
demo.erase(iter, std::end(demo));
for (auto first = demo.begin(); first < demo.end(); ++first)
{
cout << *first << " ";
}
cout << endl;
demo.shrink_to_fit();
// 输出剩余的元素
cout << "size is :" << demo.size() << endl;
cout << "capacity is :" << demo.capacity() << endl;
return 0;
}
// 1 4 5
// size is :3
// capacity is :3
std::remove
函数对其他容器同样有效。当使用 std::array
时,其不支持 erase
操作,因为其内存空间固定,无法进行自动化处理。因为 std::remove
只是将要删除的元素移动到容器末尾,而不是将其真正删除,所以这个函数也可以用于不支持空间大小变化的数据类型(如 list 之类的)。