美文网首页
1.set容器构造和赋值

1.set容器构造和赋值

作者: lxr_ | 来源:发表于2021-04-19 10:20 被阅读0次
    #include<iostream>
    using namespace std;
    
    #include<set>
    
    //set/multiset属于关联式容器,底层结构是用二叉树实现的
    //所有元素都会在插入时自动被排序
    
    //set和multiset区别:
    //set不允许容器中有重复的元素
    //multiset允许容器中有重复的元素
    
    //构造:
    //set<T> st;默认构造函数
    //set(const set& st);拷贝构造函数
    
    //赋值:
    //set& operator=(const set& st);重载=
    
    void PrintSet(set<int> st)
    {
        for (set<int>::const_iterator it = st.begin(); it != st.end(); it++)
        {
            cout << (*it) << " ";
        }
        cout << endl;
    }
    void test0101()
    {
        set<int> s1;
    
        //插入数据只有insert方式
        s1.insert(10);
        s1.insert(30);
        s1.insert(30);
        s1.insert(40);
        s1.insert(50);
        s1.insert(30);
    
        //遍历容器
        //set容器特点:所有元素被插入时自动排序,不允许插入重复值
        PrintSet(s1);
    
        set<int> s2(s1);
        PrintSet(s2);
    
        set<int> s3;
        s3 = s2;
        PrintSet(s3);
    }
    
    int main()
    {
        test0101();
    
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:1.set容器构造和赋值

          本文链接:https://www.haomeiwen.com/subject/gbfilltx.html