⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类

这篇具有很好参考价值的文章主要介绍了⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  ⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类 C++ 表情包趣味教程 👉 《C++要笑着学》⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

  • 💭 写在前面:好久没更新了,C++ 系列教程的更新速度真的是快赶得上 "年更" 了,最近这个专栏订阅量破 1200 了,也有不少小伙伴要接着往下看,所以我得好好更新!不能烂尾啊是吧。上一章我们讲解了二叉搜索树,本章我们将继续讲解 STL,介绍 set 类和 multiset,对一些常用的接口进行讲解,为后续讲解红黑树数据结构做必要的铺垫。

目录

Ⅰ. Set 类

0x00 引入:Set 介绍

0x01 元素的插入(insert 接口)

0x02 Set 同样支持范围 for

0x03 元素的查找(find 接口)

0x04 判断元素是否在集合中(count 接口)

0x05 元素的删除(erase 接口)

0x06 区间查找:lower_bound 和 upper_bound 接口

Ⅱ. multiset 类

0x00 引入:不去重的 set

0x01 multiset 的用法

0x02 multiset 的删除(erase 接口)

0x03 multiset 的查找(find 接口)


Ⅰ. Set 类

0x00 引入:Set 介绍

template < class T,                     // set::key_type/value_type
           class Compare = less<T>,     // set::key_compare/value_compare
           class Alloc = allocator<T>   // set::allocator_type
           > class set;

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类 通过插入新的元素来扩展容器,有效地通过插入的元素数量增加容器的大小。

因为集合中的元素是唯一的,插入操作检查每个插入的元素是否等同于已经在容器中的元素。

如果是,则不插入该元素,返回一个到这个现有元素的迭代器(如果该函数返回一个值)

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类 在内部,集合容器保持其所有元素按照其比较对象指定的标准进行排序。

这些元素总是按照这个排序插入到它各自的位置, 是 "排序 + 去重" 的。

  • 🔍 官方文档:https://cplusplus.com/reference/set/set/
  • 📜 头文件:#include <set>

 的底层是二叉搜索树再平衡的,是基于红黑树实现的,可归类到二叉搜索树中:

 "Sets are typically implemented as binary search trees (官方文档) "

0x01 元素的插入(insert 接口)

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类下面我们来简单来一下  的插入,调用的 insert 接口:

pair<iterator,bool> insert (const value_type& val);
iterator insert (iterator position, const value_type& val);

template <class InputIterator>  
    void insert (InputIterator first, InputIterator last);

💬 代码演示:insert() 

  • 我们通过 insert 接口将数据插入到  中,然后使用迭代器将数据打印出来。
void test_set1() {
    set<int> s;
    s.insert(4);
    s.insert(5);
    s.insert(2);
    s.insert(1);
    s.insert(3);
    s.insert(3);

    // 排序 + 去重
    set<int>::iterator it = s.begin();
    while (it != s.end()) {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
}

int main(void)
{
    test_set1();

    return 0;
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

我们发现  确实有去重的效果,这和数学中的 集合 (set) 是一样的,集合元素具有 "惟一性" 。

所以,如果插入的元素已经存在于  里了,就不插入了。

不仅如此,我们是按照 4,5,2,1,3,3 的顺序依次插入的,它还会自动帮你排好序:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类因此, 是 "排序 + 去重" 的!

0x02 Set 同样支持范围 for

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类很好!既然  支持迭代器,那自然也是支持范围 for 的!

从底层的角度 "范围 for" 其实就是迭代器,编译器处理完之后都是一样的,所以自然也是支持的。

💬 代码演示:使用范围 for

void test_set1() {
    set<int> s;
    s.insert(4);
    s.insert(5);
    s.insert(2);
    s.insert(1);
    s.insert(3);
    s.insert(3);


    // 支持范围 for
    for (auto e : s) {
        cout << e << " ";
    }
    cout << endl;
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

0x03 元素的查找(find 接口)

下面再介绍一下 find 接口,如果这个元素被找到就会返回 val 的迭代器,否则返回 end

iterator find (const value_type& val) const;

💬 代码演示:find 的使用

void test_set2() {
    set<int> s;
    s.insert(4);
    s.insert(5);
    s.insert(2);
    s.insert(1);
    s.insert(3);
    s.insert(3);

    set<int>::iterator pos = s.find(2);
    if (pos != s.end()) {
        cout << "找到了" << endl;
    }
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

我们现在用下算法中的 find 接口,看看和  中的 find 的写法有什么区别?哪个更好:

pos = find(s.begin(), s.end(), 2);
if (pos != s.end()) {
    cout << "找到了" << endl;
}

algorithm 中的 find 是暴力查找的方式实现的,从 begin 查到 end,其时间复杂度为 。

而  中的 find 的时间复杂度是  ,查找效率的差别可谓是天壤之别!

"谅尔等腐草之荧光,如何比得上天空之皓月?"

0x04 判断元素是否在集合中(count 接口)

介绍一下 count 接口,有时我们需要判断元素在不在集合中,使用 count 往往比使用 find 方便。

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类 count 非常的简单粗暴,如果在集合中则返回1,不在则返回 0。

size_type count (const value_type& val) const;

💬 代码演示:查看 3 在不在集合中

void test_set3() {
    set<int> s;
    s.insert(4);
    s.insert(5);
    s.insert(2);
    s.insert(1);
    s.insert(3);
    s.insert(3);

    if (s.count(3)) {
        cout << "3在" << endl;
    }
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类这里如果用 find 就没这么方便了,你还需要做一个判断:

if (s.find(3) != s.end()) {
    cout << "3在" << endl;
}

0x05 元素的删除(erase 接口)

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类erase 支持三种,分别是在某个位置删除,删除一个值和删除一个区间:

void erase (iterator position);
size_type erase (const value_type& val);
void erase (iterator first, iterator last);

我们下面先讲第三种,删除一个区间,实际上这一种用的是比较少的,因为比较麻烦……

💬 代码演示:通过 find 查找 pos 位置,然后删除(迭代器)

void test_set2() {
    set<int> s;
    s.insert(4);
    s.insert(5);
    s.insert(2);
    s.insert(1);
    s.insert(3);
    s.insert(3);

    cout << "当前集合元素: ";
    for (auto e : s) {
        cout << e << " ";
    }
    cout << endl;
    
    int x = 0;
    while (1) {
        cout << "请输入要删除的元素:";
        cin >> x;

        // 查看要删除的元素在不在集合中
        set<int>::iterator pos = s.find(x);
        if (pos != s.end()) {
            // 在集合中,删除
            s.erase(pos);
            cout << "成功删除: " << x << endl;

            // 打印删除后的结果
            cout << "删除后: ";
            for (auto e : s) {
                cout << e << " ";
            }
            cout << endl;
        }
        else {
            // 不在集合中,提示删除失败
            cout << "删除失败!" << x << "不在集合中。" << endl;
        }
    }
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

当然了,还可以直接给 erase 一个值进行删除,这个用法相较于迭代器就简单许多!

"在就删,不在就不动。"

💬 代码演示:erase 直接删除给定元素

void test_set3() {
    set<int> s;
    s.insert(4);
    s.insert(5);
    s.insert(2);
    s.insert(1);
    s.insert(3);
    s.insert(3);

    cout << "当前集合元素: ";
    for (auto e : s) {
        cout << e << " ";
    }
    cout << endl;
    
    s.erase(3);   // 直接删除3
    cout << "删除后: ";
    for (auto e : s) {
        cout << e << " ";
    }
    cout << endl;
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

❓ 思考:那迭代器的方式删除和直接删除有什么区别呢?

  • 💡 解答:迭代器 find + erase(pos) 的是找到了再删,我们一般和 find 搭配使用,因为如果这个元素不存在我们还强硬调用 erase 删除就会引发报错。而 erase(x) 就比较好了,直接删,就算这个值不存在也不会报错。

0x06 区间查找:lower_bound 和 upper_bound 接口

"适合确定范围"

iterator lower_bound(const key_type& key);

lower_bound() 用于在指定区域内查找 不小于 (大于等于) 目标值的第一个元素。

返回一个指向集合中第一个大于等于给定值的元素的迭代器,如果找不到,则返回 end

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类 即获取集合中任意元素的下界:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

💬 代码演示: lower_bound 查找第一个不小于 25 的元素

#include <iostream>
#include <set>
using namespace std;

int main(void) 
{
    set<int> s = { 10, 20, 30, 40, 50 };

    // 使用 lower_bound 查找第一个不小于 25 的元素
    auto it = s.lower_bound(25);

    // 输出结果
    if (it != s.end()) {
        cout << "第一个不小于 25 的元素是: " << *it << endl;
    }
    else {
        cout << "未找到不小于 25 的元素" << endl;
    }

    return 0;
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

  • 🔑 解读:lower_bound(25) 会返回一个指向集合中第一个不小于 25 的元素的迭代器。在集合{10, 20, 30, 40, 50} 中,第一个不小于 25 的元素是 30,所以输出结果是30。

可以用来干什么呢?如果我们需要删除大于等于 x 的所有元素,我们就可以用到这个接口了。

💬 代码演示:删除大于等于 x 的所有元素:⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类 

void test_set4() {
    set<int> s = { 10, 20, 30, 40, 50 };
    cout << "删除前: ";
    for (auto e : s) {
        cout << e << " ";
    } cout << endl;


    int x = 0;
    cout << "请输入x: ";
    cin >> x;

    auto it = s.lower_bound(x);  // 找到第一个不小于x的元素
    s.erase(it, s.end());        // 迭代器区间删除

    cout << "删除后: ";
    for (auto e : s) {
        cout << e << " ";
    } cout << endl;
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

  • 🔑 解读:接收 x 的值为 30 之后,就从 lower_bound 的位置开始作为迭代器的起始位置,end 作为结束位置,调用迭代器版本的 erase 就可以做到 30, 40, 50 都删除的效果,最后保留 10, 20。

还有一个 upper_bound 接口,在指定范围内查找大于目标值的第一个元素,不包含等于。

 ⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类 这两个接口就是为了迎合迭代器的 "左闭右开" 而配备的,可以结合使用。

💬 代码演示:删除区间   

void test_set5() {
    set<int> s = { 10, 20, 30, 40, 50 };
    cout << "删除前: ";
    for (auto e : s) {
        cout << e << " ";
    } cout << endl;
        

    int x = 0, y = 0;
    cin >> x >> y;
    auto left_it = s.lower_bound(x);
    auto right_it = s.upper_bound(y);
    s.erase(left_it, right_it);

    cout << "删除后: ";
    for (auto e : s) {
        cout << e << " ";
    } cout << endl;
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类 感兴趣的朋友可以看下 lower_bound 的底层实现:

template <class ForwardIterator, class T>
ForwardIterator lower_bound (
    ForwardIterator first, 
    ForwardIterator last, 
    const T& val
    )
{
    ForwardIterator it;
    iterator_traits<ForwardIterator>::difference_type count, step;
    count = distance(first,last);
    while (count>0)
    {
        it = first; step=count/2; advance (it,step);
        if (*it<val) {
            first=++it;
            count-=step+1;
        }
        else count=step;
    }
    return first;
}

emmm,是个很经典的二分查找算法!使用了 advance 函数来跳过部分区间~

Ⅱ. multiset 类

0x00 引入:不去重的 set

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类如果我们想让  不去重,我们就可以使用 ,使用方法和 Set 大差不差。

Set 是 "排序 + 去重",但 multiset 是 "排序 + 不去重",也就是说 multiset 允许元素重复!

0x01 multiset 的用法

multiset 就在 Set 类中,所以头文件就是 #include <set> 

💬 代码演示:multiset 的插入

#include <iostream>
#include <set>
using namespace std;

void test() {
    multiset<int> s;
    s.insert(4);
    s.insert(5);
    s.insert(2);
    s.insert(1);
    s.insert(3);
    s.insert(3);


    // 支持范围 for
    for (auto e : s) {
        cout << e << " ";
    }
    cout << endl;
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类可以看到,是不去重的,就给你排个序,已经有的元素也会继续插入。

0x02 multiset 的删除(erase 接口)

因为 Set 不重复,所以 erase 就删除指定元素,但 multiset 是允许冗余的。

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类所以 multiseterase 接口会把所有指定元素删掉:

void test() {
    multiset<int> s;
    s.insert(4);
    s.insert(5);
    s.insert(2);
    s.insert(1);
    s.insert(3);
    s.insert(3);

    cout << "删除前: ";
    for (auto e : s) {
        cout << e << " ";
    }
    cout << endl;

    s.erase(3);    // 会把所有的3都杀了

    cout << "删除后: ";
    for (auto e : s) {
        cout << e << " ";
    }
    cout << endl;
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

0x03 multiset 的查找(find 接口)

multisetfind 如果要查找的元素在集合中有多个,会返回中序的第一个,假设我们要 find(3)

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

💬 代码演示:find 接口

void test() {
    multiset<int> s;
    s.insert(4);
    s.insert(5);
    s.insert(2);
    s.insert(1);
    s.insert(3);
    s.insert(3);
    s.insert(3);   // 一共有3个3

    for (auto e : s) {
        cout << e << " ";
    }
    cout << endl;

    // 返回的是中序的第一个
    auto pos = s.find(3);
    while (pos != s.end()) {
        cout << *pos << " ";
    }
}

🚩 运行结果如下:

⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类


⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类,C++要笑着学,c++,开发语言,C++要笑着学,set类,multiset类

📌 [ 笔者 ]   王亦优
📃 [ 更新 ]   2023.12.12
❌ [ 勘误 ]   /* 暂无 */
📜 [ 声明 ]   由于作者水平有限,本文有错误和不准确之处在所难免,
              本人也很想知道这些错误,恳望读者批评指正!

📜 参考资料 

C++reference[EB/OL]. []. http://www.cplusplus.com/reference/.

Microsoft. MSDN(Microsoft Developer Network)[EB/OL]. []. .

百度百科[EB/OL]. []. https://baike.baidu.com/.

比特科技. C++[EB/OL]. 2021[2021.8.31]. 文章来源地址https://www.toymoban.com/news/detail-767774.html

到了这里,关于⚡【C++要笑着学】(30) 集合类:set 类 | 元素的插入和删除 | lower_bound 接口 | upper_bound 接口 | multiset 类的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 探索Java中的Set集合:独一无二的元素之旅

     在Java编程中,我们经常需要处理一组元素,并且确保其中没有重复的元素。为了满足这一需求,Java提供了一个强大的数据结构——Set集合。Set集合是一种无序且不允许重复元素的集合,提供了高效的去重和查找功能。本文将深入探索Java中的Set集合,介绍其基本概念、常见

    2024年02月16日
    浏览(39)
  • O(1)插入、删除和随机元素[中等]

    优质博文:IT-BLOG-CN 实现 RandomizedSet 类: 【1】 RandomizedSet() 初始化 RandomizedSet 对象。 【2】 bool insert(int val) 当元素 val 不存在时,向集合中插入该项,并返回 true ;否则,返回 false 。 【3】 bool remove(int val) 当元素 val 存在时,从集合中移除该项,并返回 true ;否则,返回 fal

    2024年01月19日
    浏览(26)
  • List集合删除指定元素-四种方法

    第一种 for循环 输出结果为 第二种 迭代器 输出结果为 第三种 stream流 这种过滤是留下符合条件的元素 输出结果为 第四种 removeIf 方法以及 方法引用 removeIf 方法是jdk1.8 Collection以及其子类新增的,作用是过滤指定条件的元素 输出结果为 总结 不言而喻,第四种方法最好用。

    2024年02月09日
    浏览(43)
  • STL--list如何实现元素的插入和删除

    在C++标准模板库(STL)中,std::list 是一个双向链表。由于它的双向链表特性,std::list 支持在任何位置高效地插入和删除元素。 元素插入: ●使用 push_back() 在列表尾部添加元素; ●使用 push_front() 在列表头部添加元素; ●使用 insert() 在指定位置插入元素。这需要一个迭代器

    2024年04月12日
    浏览(27)
  • 【Java基础教程】(四十八)集合体系篇 · 上:全面解析 Collection、List、Set常用子接口及集合元素迭代遍历方式~【文末送书】

    掌握 Java 设置类集的主要目的以及核心接口的使用; 掌握 Collection 接口的作用及主要操作方法; 掌握 Collection 子接口 List、Set 的区别及常用子类的使用; 掌握 Map 接口的定义及使用; 掌握集合的4种输出操作语法结构; 掌握 Properties类的使用 ; 了解类集工具类 Collections 的作

    2024年02月15日
    浏览(45)
  • 算法:O(1) 时间插入、删除和获取随机元素---哈希表+动态数组

    1、题目: 实现 RandomizedSet 类: RandomizedSet() 初始化 RandomizedSet 对象 bool insert(int val) 当元素 val 不存在时,向集合中插入该项,并返回 true ;否则,返回 false 。 bool remove(int val) 当元素 val 存在时,从集合中移除该项,并返回 true ;否则,返回 false 。 int getRandom() 随机返回现有

    2024年02月08日
    浏览(39)
  • 任何时候都不要在 for 循环中删除 List 集合元素!!!

    首先说结论:无论什么场景,都不要对List使用for循环的同时,删除List集合元素,因为这么做就是不对的。 阿里开发手册也明确说明禁止使用foreach删除、增加List元素。 正确删除元素的方式是使用迭代器(Iterator),代码如下: JDK8后lambda写法: 不想知道为什么不能使用for循

    2023年04月15日
    浏览(36)
  • List 集合遍历过程中删除元素。这个坑踩一遍就不要再踩了!

    作为一名后端开发,不管采用什么语言 使用 List 集合的频率都非常高。 对 List 集合的遍历和遍历中操作数据也是家常便饭。 我从我使用的过程中对于此问题的思考与实践形成记录,与大家交流。有不对的地方,恳请大家指正。 List 集合的遍历有很多种方式,每一种遍历方式

    2024年02月12日
    浏览(33)
  • 【C++】红黑树插入删除

    喜欢的点赞,收藏,关注一下把! 红黑树 ,是一种 二叉搜索树 ,但 在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对 任何一条从根到叶子的路径上各个结点着色方式的限制 , 红黑树确保没有一条路径会比其他路径长出俩倍 ,因而是 接近平衡 的。

    2024年02月02日
    浏览(31)
  • 【C++】string字符串查找替换、比较、提取、插入和删除

    Link 加油! 感谢! 努力!

    2024年02月12日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包