• stl.set用法总结


    0 -- 内部类型定义
    template <class Key, class Compare = less<Key>, class Alloc = alloc>
    class set
    {
    ......
    private:
    typedef rb_tree<key_type identity=""  ,=""><value_type value_type,="">, key_compare, Alloc> rep_type;
    rep_type t;  //set内部的实现实际上是一个红黑树
    ......
    };
    typedef Key key_type; 关键字类型
    typedef Key value_type; 值类型(size_t)。
    typedef Compare key_compare; 关键字比较函数
    typedef Compare value_compare; 值比较函数
    typedef typename rep_type::const_pointer pointer; 指针类型
    typedef typename rep_type::const_pointer const_pointer; const指针类型
    typedef typename rep_type::const_reference reference; 引用类型
    typedef typename rep_type::const_reference const_reference; const引用类型
    typedef typename rep_type::const_iterator iterator; 迭代器类型
    typedef typename rep_type::const_iterator const_iterator; const迭代器类型
    typedef typename rep_type::const_reverse_iterator reverse_iterator; 反向迭代器类型
    typedef typename rep_type::const_reverse_iterator const_reverse_iterator; const反向迭代器类型
    typedef typename rep_type::size_type size_type; 表示容量等数值类型
    ypedef typename rep_type::difference_type difference_type; 表示元素位置差距的数值类型
    1 -- 构造函数
    set()

    构造一个空的set容器。
    set(const Compare& comp)

    构造一个空的set容器。使用comp作为关键字比较函数。
    set(const set<Key, Compare, Alloc> & x)

    使用x初始化一个set容器。
    template <class InputIterator>
    set(InputIterator first, InputIterator last)


    使用迭代器[first, last)指代的元素构造set容器。
    template <class InputIterator>
    set(InputIterator first, InputIterator last, const Compare & comp)


    使用迭代器[first, last)指代的元素构造set容器。并使用comp作为关键字比较函数。
    2 -- 元素操作函数
    set<Key, Compare, Alloc> & operator=(const set<Key, Compare, Alloc> & x)

    使用容器x给当前容器赋值。
    void swap(set<Key, Compare, Alloc> & x)

    交换两个容器的值。
    pair<iterator, bool> insert(const value_type& x)

    将一个元素插入当前set容器。返回值pair的第一个元素,表示插入位置的迭代器;第二个元素,表示是否插入到容器中,只有该值为true,迭代器才有效。
    iterator insert(iterator position, const value_type& x)

    将值x插入当前容器中,然会新元素位置。position是个提示,指出插入操作的搜寻起点。如果提示恰当,可大大加快速度。
    template <class InputIterator>
    void insert(InputIterator first, InputIterator last)


    将迭代器[first,last)指代的元素插入当前容器中。
    void erase(iterator pos)

    删除迭代器指向的元素。
    size_type erase(const key_type & k)

    删除关键字为k的元素。返回删除的元素数目。
    void erase(iterator first, iterator last)

    删除迭代器[first, last)指代的元素。
    void clear()

    清空当前容器。
    3 -- 元素访问函数(迭代器、引用及访问方法)
    iterator begin() const

    返回的迭代器指向set的第一个元素。使用(*iterator)即引用对应值。
    iterator end() const

    返回的迭代器指向set的“末端元素的下一个元素”。
    reverse_iterator rbegin() const

    返回反向迭代器,指向set的最后一个元素。
    reverse_iterator rend() const

    返回反向迭代器,指向set第一个元素的前一个位置。
    iterator find(const key_type & x) const

    查找关键字x在set中的位置,并返回对应迭代器。如果没有找到则返回end()指向的迭代器。
    iterator lower_bound(const key_type & x) const

    返回容器中关键字不小于x(满足<=x关系)的第一个元素的迭代器。
    iterator upper_bound(const key_type & x) const

    返回容器中关键字大于x(满足>x关系)的第一个元素的迭代器。
    pair<iterator,iterator> equal_range(const key_type & x) const

    返回容器中关键字等于x(满足==x关系)的元素区间。返回值[pair.first, pair.second]均满足该关系。
    size_type count(const key_type & x) const

    计算关键字x在set中的个数。对于set而言,只存在1(表示关键字在set中)或0(关键字不在set中)两种情况。
    4 -- 容器属性函数
    bool empty() const

    判断当前容器是否为空。没有元素时返回true否则false。
    size_type size() const

    获取当前容器包含元素的个数。
    size_type max_size() const

    获取当前容器最大可能容量。一般值为size_type(-1)。
    key_compare key_comp() const

    返回用于关键字比较的仿函数对象。
    value_compare value_comp() const

    返回用于值比较的反函数对象。
    5 -- 容器比较函数
    template <class Key, class Compare, class Alloc>
    inline bool operator == (const set<Key, Compare, Alloc> & x, const set<Key, Compare, Alloc> & y)


    判断两个set容器是否相等。全局函数,非成员函数。
    template <class Key, class Compare, class Alloc>
    inline bool operator < (const set<Key, Compare, Alloc> & x, const set<Key, Compare, Alloc< & y)


    判断容器x是否小于y。全局函数,非成员函数。
    6 -- 容器使用例子

    使用set容器需要注意的几个地方:

    (1)std::set不提供下标操作符;

    (2)如果只是判断元素是否存在,可以使用count函数检查返回值;

    (3)如果需要获取元素值,可使用迭代器。*iterator就是该迭代器指向的值;

    (4)如果当前容器中的元素是自己new出来的对象指针,那么在调用clear或erase函数之前,需要自己先释放掉这部分内存。

    myclass * p1 = new myclass();
    myclass * p2 = new myclass();
    myclass * p3 = new myclass();
    std::set set_class;
    set_class.insert(p1);
    set_class.insert(p2);
    set_class.insert(p3);
    //在调用clear之前,先依次delete对象。否则会有内存泄露。
    for (std::set::iterator it = set_class.begin(); it != set_class.end(); it++)
    delete *it;
    set_class.clear();
    
  • 相关阅读:
    4 linux上运行crm&uwsgi
    Vue 目录
    vue -(滚动播放-全屏展示)
    Chrome 调试技巧
    Vue 遇到的坑
    9 matplotlib
    Maven
    Dev-C++黑暗主体和代码高亮配置
    Git操作:一次push把代码提交到两个仓库
    windows server 2012 r2 快速启动 部署remoteapp 显示服务器有挂起的重启,怎么解决?
  • 原文地址:https://www.cnblogs.com/motadou/p/1626127.html
Copyright © 2020-2023  润新知