C++(11): set与multiset

目录

1. 简述

2. std::set

3. API

(1)元素插入

(2)元素删除

(3)特定元素查找

(4)清空元素

(5)元素个数

(6)set能够容纳的最大元素数

(7)判空

(8)迭代

(9)set内容交换

(10)比较操作

4. 例程

5. multiset

6. multiset例程


1. 简述

        在C++标准模板库(STL)中,std::set是一个基于平衡二叉搜索树(通常是红黑树)的关联容器,它能够存储唯一的元素并保持这些元素按排序顺序。由于std::set内部通常使用红黑树(一种自平衡的二叉搜索树)来实现,所以保证高效的查找、插入和删除操作。

2. std::set

        std::set的原型如下。

template < class T, class Compare = less<T>, class Allocator = allocator<T> >  

class set;

其中:

        T 是存储在集合中的元素的类型。

        Compare 是一个二元谓词,用于比较两个元素。默认是 std::less<T>,即按照升序排序。

        Allocator 是用于管理存储的分配器对象。默认是 std::allocator。

        std::set的构造函数如下。

 set(); ///< 默认构造函数

set(const set& other); ///< 拷贝构造函数

set(set&& other); ///< 移动构造函数

set(std::initializer_list<value_type> init); ///< 使用初始化列表构造

explicit set(const Compare& comp); // 指定比较函数的构造函数

set(const Compare& comp, const Allocator& alloc); ///< 指定比较函数和分配器的构造函数 

3. API

        std::set提供了丰富的成员函数来操作集合,以下是一些常用的API函数:

(1)元素插入

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

        向集合中插入一个新的元素。

        iterator insert(const_iterator hint, const value_type& val):

        在 hint 提供的位置附近插入 val,提高插入效率。

(2)元素删除

        erase(value_type val):从集合中删除一个元素。

(3)特定元素查找

        const_iterator find(const key_type& key):查找与 key 相匹配的元素,返回一个迭代器指向找到的元素或 end() 如果未找到。

        const_reference at(size_type pos):访问指定位置 pos 的元素,如果 pos 超出范围,抛出 std::out_of_range。

(4)清空元素

        void clear():清空集合中的所有元素。

(5)元素个数

        size_type size() const:返回集合中元素的数量。

(6)set能够容纳的最大元素数

        size_type max_size() const;

(7)判空

        bool empty() const:检查集合是否为空。

(8)迭代

        iterator begin():返回指向 set 开始的迭代器。

        const_iterator begin() const:返回指向 set 开始的常量迭代器。

        iterator end():返回指向 set 结束的迭代器(尾后一个位置)。

        const_iterator end() const:返回指向 set 结束的常量迭代器。

(9)set内容交换

        void swap(set& other):与 other 交换内容。

(10)比较操作

        bool operator==(const set& other) const:判断当前 set 是否与 other 相等。

        bool operator!=(const set& other) const:判断当前 set 是否与 other 不相等。

        bool operator<(const set& other) const:判断当前 set 是否小于 other。

        bool operator<=(const set& other) const:判断当前 set 是否小于等于 other。

        bool operator>(const set& other) const:判断当前 set 是否大于 other。

        bool operator>=(const set& other) const:判断当前 set 是否大于等于 other。

4. 例程

#include <iostream>  

#include <set>  

 

int main(int argc, char* argv[])

{  

    /** 创建一个存储整数的set容器 */  

    std::set<int> mySet;  

 

    /** 向set中插入元素 */  

    mySet.insert(5);  

    mySet.insert(3);  

    mySet.insert(9);  

    mySet.insert(1);  

    mySet.insert(7);  

    mySet.insert(3); ///< 尝试插入重复元素,将不会成功  

 

    /** 输出set中的元素 */

    for (int num : mySet) {  

        std::cout << num << " ";  

    }  

    std::cout << std::endl; ///< 输出:1 3 5 7 9 (已排序,且无重复)  

 

    /** 查找元素 */

    if (mySet.find(7) != mySet.end()) {  

        std::cout << "Element 7 found in the set." << std::endl;  

    } else {  

        std::cout << "Element 7 not found in the set." << std::endl;  

    }  

 

    /** 删除元素 */ 

    mySet.erase(5);  

    std::cout << "After erasing 5, the set contains:" << std::endl;  

    for (int num : mySet) {  

        std::cout << num << " ";  

    } ///< 输出:1 3 7 9 (5已被删除)  

 

    return 0;  

}

5. multiset

        multiset与set类似,都是基于红黑树的结构,不同的是,multiset允许存储多个值相同的元素。

        其原型如下。

template< class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key> >  

class multiset;

        multiset提供了丰富的成员函数来进行元素的插入、删除、查找等操作。例如:

        插入元素:使用insert()函数可以插入一个或多个元素到multiset中。

        删除元素:使用erase()函数可以删除指定元素或指定范围内的元素。

        查找元素:使用find()函数可以查找指定元素,并返回指向该元素的迭代器。此外,还可以使用lower_bound()和upper_bound()函数来查找等值区间的起始和结束位置。

        遍历元素:可以使用迭代器来遍历multiset中的所有元素。

6. multiset例程

#include <iostream>  

#include <set>  

 

int main(int argc, char* argv[])

{  

    /** 创建一个multiset容器 */  

    std::multiset<int> myMultiset;  

 

    /** 插入元素,包括重复值  */ 

    myMultiset.insert(30);  

    myMultiset.insert(20);  

    myMultiset.insert(30); ///< 插入重复值  

    myMultiset.insert(10);  

    myMultiset.insert(30); ///< 再次插入重复值  

    myMultiset.insert(40);  

 

    /** 查找值为30的元素  */ 

    auto itFind = myMultiset.find(30);  

    if (itFind != myMultiset.end()) {  

        std::cout << "Found " << *itFind << " in the multiset." << std::endl;  

    }  

 

    /** 使用lower_bound和upper_bound遍历所有值为30的元素  */ 

    auto itLower = myMultiset.lower_bound(30);  

    auto itUpper = myMultiset.upper_bound(30);  

 

    std::cout << "All elements with value 30 in the multiset:" << std::endl;  

    for (auto it = itLower; it != itUpper; ++it) {  

        std::cout << *it << " ";  

    }  

    std::cout << std::endl;  

 

    /** 输出multiset中的所有元素 */  

    std::cout << "All elements in the multiset:" << std::endl;  

    for (const auto& elem : myMultiset) {  

        std::cout << elem << " ";  

    }  

    std::cout << std::endl;  

 

    return 0;  

}

  • 24
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值