C++标准库学习之 STL Removing 操作

在研究 STL 的remove 操作时,有很多让我非常意外的问题,使用下面例子来说一下

c 复制代码
#include <iostream>
#include <list>
#include <algorithm>

int main(){


    // 创建一个向量,带有初始内容
    std::list<int> tsm_vector ={1,2,3,4,5,6,7,8,9};

    // 先删除  5 这个节点,删除成功后,尾结点会向前移动
    auto remove=    std::remove(tsm_vector.begin(),tsm_vector.end(),5);

    for (auto item:tsm_vector){
        std::cout<< item<<std::endl;
    }

    return 0;
}

结果:

vbnet 复制代码
D:\CWorkSpace\tsmTest\cmake-build-debug\tsmTest.exe
1
2
3
4
6
7
8
9
9

Process finished with exit code 0

在删除后发现所有节点都向前移动了,但是最后一个节点他没管,看了一下他的源码,发现他的源码就是不停的向后遍历,

那么该如何删除掉最后这个无用的9呢,

c 复制代码
#include <iostream>
#include <list>
#include <algorithm>

int main(){

    // 创建一个向量,带有初始内容
    std::list<int> tsm_vector ={1,2,3,4,5,6,7,8,9};

    // 先删除  5 这个节点,删除成功后,尾结点会向前移动
    auto remove=    std::remove(tsm_vector.begin(),tsm_vector.end(),5);

    // 在使用 erase 方法擦除无用节点
    tsm_vector.erase(remove,tsm_vector.end());
    
    for (auto item:tsm_vector){
        std::cout<< item<<std::endl;
    }

    return 0;
}

在使用完 remove 后,这个方法会返回他删除后最新的最后一个节点,也就是新的 end ,我们只需要使用 erase[remove,eng) 这个区间就可以了

那我们能直接使用 erase 直接参数吗, 答案肯定是可以的,

c 复制代码
#include <iostream>
#include <list>
#include <algorithm>

int main() {


    // 创建一个向量,带有初始内容
    std::list<int> tsm_vector = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    // 先删除  5 这个节点,删除成功后,尾结点会向前移动
    auto remove = std::remove(tsm_vector.begin(), tsm_vector.end(), 5);

    // 在使用 erase 方法擦除无用节点
    tsm_vector.erase(remove, tsm_vector.end());

    for (auto item:tsm_vector) {
        std::cout << item << std::endl;
    }


    std::cout << "----------------使用 remove 后,在使用 erase-------------------" << std::endl;

    std::cout << "----------------直接使用 erase-------------------" << std::endl;

    std::list<int>::iterator start = tsm_vector.begin();
    
    while (start!=tsm_vector.end()){
        if (*start % 2 == 0) {
            // 使用擦出后的地址继续遍历,否则原来的start 会指向野指针,导致while 循环出错,
            start=tsm_vector.erase(start);
        } else {
            start++;
        }
    }

    for (auto item:tsm_vector) {
        std::cout << item << std::endl;
    }

    return 0;
}

为什么remove 无法删除数据而 erase 可以呢,在 STL 算法的设计初衷就是为了程序的扩展性等等情况考虑的, remove 等方法是与 容器脱离的,而erase 是容器自身的方法,容器自身肯定是知道容器内部的情况的,所以就形成了现在的情况

上面的例子我们使用的是 std::remove 方法,这个是 algorithm 算法库为我们提供的一个方法,想要更方便的移除该如何操作呢,

那就是调用容器的remove 方法

arduino 复制代码
std::list<int> tsm_vector = {1, 2, 3, 4, 5, 6, 7, 8, 9};

// 调用 tsm_vector 这个容器的 remove 方法即可快速的移除数据
tsm_vector.remove(5);
相关推荐
一个不知名程序员www3 小时前
算法学习入门 --- 哈希表和unordered_map、unordered_set(C++)
c++·算法
C++ 老炮儿的技术栈4 小时前
在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?
c语言·c++·windows·git·vscode·visual studio
%xiao Q4 小时前
GESP C++五级-202406
android·开发语言·c++
Sarvartha4 小时前
C++ STL 栈的便捷使用
c++·算法
Aevget5 小时前
MFC扩展库BCGControlBar Pro v37.2 - 全新的VS 2026可视化管理器
c++·mfc·bcg·界面控件·ui开发
C+-C资深大佬5 小时前
C++类型判断
开发语言·c++
Yu_Lijing5 小时前
基于C++的《Head First设计模式》笔记——模式合作
c++·笔记·设计模式
zmzb01036 小时前
C++课后习题训练记录Day74
开发语言·c++
cdut_suye6 小时前
解锁函数的魔力:Python 中的多值传递、灵活参数与无名之美
java·数据库·c++·人工智能·python·机器学习·热榜
txinyu的博客7 小时前
前置声明与 extern
linux·c++