擦除/移除std::vector元素

std::remove函数并不实际删除元素,而是将删除元素移到容器末尾。要真正移除元素,需结合使用erase。在删除后,使用shrink_to_fit可以回收多余容量。对于固定大小的容器如std::array,不能使用erase,std::remove则足够。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

擦除/移除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 之类的)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值