zoukankan      html  css  js  c++  java
  • set相关

    1. 定义

    #include <set>
    
    template<
        class Key,
        class Compare = std::less<Key>,
        class Allocator = std::allocator<Key>
    > class set;
    

    2. 初始化

    1. 默认构造,空set
    2. 范围构造,使用另一容器的迭代器进行构造
    3. 拷贝构造,拷贝另一个set
    4. 移动构造,移动另一个set
    5. 初始化列表构造,使用c++11的初始化列表进行构造
    // 默认初始化,设置set属性的初始化,没有元素
    set();
    explicit set(const Compare &comp, const Allocator &alloc = Allocator());
    explicit set(const Allocator &alloc); // c++11
    
    // 使用另一个容器的迭代器进行初始化
    template< class InputIt >
    set( InputIt first, InputIt last, const Compare& comp = Compare(), const Allocator& alloc = Allocator() );
    
    template< class InputIt >
    set( InputIt first, InputIt last, const Allocator& alloc) : set(first, last, Compare(), alloc) {} // c++14
    
    // 使用另一个set初始化
    set( const set& other );
    set( const set& other, const Allocator& alloc ); // c++11
    set( set&& other );// c++11
    set( set&& other, const Allocator& alloc );// c++11
    
    // 使用初始化列表
    set( std::initializer_list<value_type> init, const Compare& comp = Compare(), const Allocator& alloc = Allocator() ); // c++11
    

    3. insert

    // 插入一个值
    std::pair<iterator, bool> insert(const value_type &value);
    std::pair<iterator, bool> insert(value_type &&value);   // c++11
    
    // 在靠近 hint 的位置插入一个值
    iterator insert(const_iterator hint, const value_type &value);
    iterator insert(const_iterator hint, value_type &&value);   // c++11
    
    // 插入多个值
    template<class InputIt>
    void insert(InputIt first, InputIt last);
    void insert(std::initializer_list<value_type> ilist);
    

    4. 取交集并集

    std::set_intersection()这组函数必须使用有序set,不能使用unorder_set

    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        set<int> s1(nums1.begin(), nums1.end());
        set<int> s2(nums2.begin(), nums2.end());
    
        vector<int> ret;
        // 交集
        std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(ret, ret.begin()));
    
        // 并集
        //std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(ret, ret.begin()));
    
        return move(ret);
    }
    
  • 相关阅读:
    numpy库:常用基本
    高考的结束,新的开始
    Hello World!
    第一篇随笔
    Linux命令之文件与用户权限
    看山不是山,看水不是水
    Python基础篇【第1篇】: Python基础
    css居中
    JS正则表达式(一)
    小问题总结
  • 原文地址:https://www.cnblogs.com/suntus/p/15063757.html
Copyright © 2011-2022 走看看