STL关联式容器之set

set的特性是,所有元素都会根据元素的键值自动被排序。set的元素不像map那样可以同时拥有实值(value)和键值(key),set元素的键值就是实值,实值就是键值。set不允许两个元素有相同的键值。

我们不能通过set的迭代器修改set的元素值。因为set元素值就是器键值,关系到奥set元素的排列规则。如果任意改变set元素值,会严重破坏set组织。稍后会在set源代码中看到,set<T>::iterator被定义为底层RB-tree的const_iterator,杜绝写入操作。换句话说,set iterator是一种constant iterators(相对于木谈了iterators)。

set拥有与list相同的性质:当客户端对它进行元素新增操作(insert)或删除操作(erase)时,操作之前的所有迭代器,在操作完成之后都依然有效。(除了被删除的那个迭代器)

STL特别提供了一组set/multiset相关算法,包括交集set_intersection,联集set_union,差集set_difference、对称差集set_symmetric_difference,后续内容会进行描述。

由于RB-tree是一种平衡二叉搜索树,自动排序的效果很不错,所以标准的STL set即以RB-tree为底层机制。又由于set所开放的各种操作接口,RB-tree也都提供了,所有几乎所有的set操作行为,都只是转调用RB-tree的操作行为而已。

下面是set的源代码摘录,其中注释几乎说明了一切,本节不在另做文字解析

cpp 复制代码
template <class Key,
          class Compare = less<Key>,
          class Alloc = alloc>
class set {
    typedef Key key_type;
    typedef Key value_type;
    typedef Compare key_compare;
    typedef Compare value_compare;
private:
// 注意一下的identiity定义于<stl_function.h>中
/*
    template <class T> 
    struct identity : public unary_function<T, T> {
        const T& operator()(const T&x) const { return x;}
    }
*/
    typedef rb_tree<key_type, value_type, identity<value_type>, key_compare, Alloc> rep_type;
    rep_type t;
public:
    typedef typename rep_type::const_pointer pointer;
    typedef typename rep_type::const_pointer const_pointer;
    typedef typename rep_type::const_reference reference;
    typedef typename rep_type::const_reference const_reference;
    typedef typename rep_type::const_iterator iterator;

    typedef typename rep_type::const_iterator const_iterator;
    typedef typename rep_type::const_reverse_iterator reverse_iterator;
    typedef typename rep_type::const_reverse_iterator const_reverse_iterator;
    typedef typename rep_type::size_type size_type;
    typedef typename rep_type::difference_type difference_type;

    set() : t(Compare()) {}
    explicit set(const Compare& comp) : t(comp) {}
    
    template <class InputIterator> 
    set(InputIterator first, InputIterator last)
    : t(Compare()) { t.insert_unique(first, last);}

    template <class InputIterator>
    set(InputIterator first, InputIterator last, const Compare& comp) 
    : t(comp){ t.insert_unique(first, last);}
    
    set(const set<Key, Compare, Alloc>& x) : t(x.t) {}
    
    set<Key, Compare, Alloc>& operator=(const set<Key, Compare, Alloc>&x) {
        t = x.t;
        return *this;
    }

    // 以下所有的set操作行为,RB-tree都以提供,所以set只要传递调用即可
    
    key_compare key_comp() const { return t.key_comp(); }
    value_compare value_comp() const { return t.key_comp(); }
    iterator begin() const { return t.begin(); }
    iterator end() const { return t.end(); }
    reverse_iterator rbegin() const { return t.rbegin(); }
    reverse_iterator rend() const { return t.rend(); }
    bool empty() const { return t.empty(); }
    size_type size() const { return t.size(); }
    size_type max_size() const { return t.max_size(); }
    void swap(set<Key, Compare, Alloc>&x) { t.swap(x.t); }

    typedef pair<iterator, bool> pair_iterator_bool;
    pair<iterator, bool> insert(cosnt value_type&x) {
        pair<typename rep_type::iterator, bool> p = t.insert_unique(x);
        return pair<iterator, bool>(p.first, p.second);
    }

    iterator insert(iterator position, const value_type &x) {
        typedef typename rep_type::iterator rep_iterator;
        return t.insert_unique((rep_iterator&)position, x);
    }

    template<class InputIterator>
    void insert(InputIterator first, InputIterator last) {
        t.insert_unique(first, last);
    }

    void erase(iterator position) {
        typedef typename rep_type::iterator rep_iterator;
        t.erase((rep_iterator&)position);
    }

    size_type erase(const key_type&x) {
        return t.erase(x);
    }

    void erase(InputIterator first, InputIterator last) {
        typedef typename rep_type::iterator rep_iterator;
         t.erase((rep_iterator&) first, (rep_iterator&) last);
    }

    void clear() { t.clear(); }

    iterator finds(const key_type&x) cosnt { return t.find(x); }
    size_type count(const key_type&x) const { return t.count(x); }
    iterator lower_bound(const key_type&x) const { return t.lower_bound(x); }
    iterator upper_bound(const key_type&x) const { return t.upper_bound(x); }
    pair<iterator, iterator> equal_range(const key_type&x) const 
    { return t.equal_range(x); }

    // 以下的__STL_NULL_TMPL_ARGS被定义为<>
    friend bool operator == STL_NULL_TMPL_ARGS (const set&, const set&);
    friend bool operator < STL_NULL_TMPL_ARGS (const set&, const set&);
    

};
cpp 复制代码
template <class Key, class Compare, class Alloc> 
inline bool operator == (const set<Key, Compare, Alloc>&x, const set<Key, Compare, Alloc>& y) {
    return x.t == y.t;
}

template <class Key, class Compare, class Alloc> 
inline bool operator < (const set<Key, Compare, Alloc>&x, const set<Key, Compare, Alloc>& y) {
    return x.t < y.t;
}
相关推荐
SEO_juper1 分钟前
2026年用Python分析网站访问日志:看清Googlebot和AI爬虫怎么爬你的站(附完整代码)
开发语言·前端·seo·独立站·谷歌优化
j7~8 分钟前
【C++微服务项目开发脚手架】(准备篇)从 0 到跑通:Docker 容器开发环境 + 宿主机用户 + Trae 远程连接 全链路踩坑实战
linux·c++·学习·xshell·docker容器·trae
唠玖馆27 分钟前
C++11
c++
邪修king1 小时前
Re:Linux 系统篇(二十九):动静态库Chapter2:动态库深度辨析 —— 核心本质、制作流程、双阶段查找模型与排错指南
android·java·linux·开发语言
泡海椒1 小时前
告别 iText 繁杂配置:jquick-pdf 极简 PDF 生成实战(零基础上手)
java·开发语言·pdf
云泽8083 小时前
从零搭建 WSL2 + Ubuntu C/C++ 开发环境:原理、实践与底层架构解析
linux·c++·windows·ubuntu·wsl2
初願致夕霞9 小时前
C++11:类型推导与完美转发复盘笔记
c++
小猪写代码9 小时前
C++中什么是类,什么是对象
c++
君顾112 小时前
上海24小时自助健身房系统开发实战指南:从架构设计到落地部署
java·开发语言·健身房
香菜TTT13 小时前
大模型上下文协议(MCP):AI 应用的“USB-C”接口技术
开发语言·人工智能·经验分享