zoukankan      html  css  js  c++  java
  • C++标准容器库STL set:

    int main()
    {
        set<Student> stuset;
        stuset.insert(Student("zhangsan",22));  //默认为升序 会自动调用<操作符重载
        stuset.insert(Student("lisi",52));
        stuset.insert(Student("wangwu",48));
    
    //    set<Student,less<Student>>stuset1;
    //    stuset1.insert(Student("zhangsan",22));  //升序会自动调用<操作符重载
    //    stuset1.insert(Student("lisi",52));
    //    stuset1.insert(Student("wangwu",48));
    
    //    set<Student,greater<Student>>stuset2;
    //    stuset2.insert(Student("zhangsan",22));  //降序会自动调用>操作符重载
    //    stuset2.insert(Student("lisi",52));
    //    stuset2.insert(Student("wangwu",48));
    
    
    //    set<Student>::iterator it = stuset.begin();       //ok    遍历
    //    for(;it!=stuset.end();it++)
    //        it->showStu();
    
        for_each(stuset.begin(),stuset.end(),show);
    //    for_each(stuset1.begin(),stuset1.end(),show);
    //    for_each(stuset2.begin(),stuset2.end(),show);
    }
    int main()
    {
        set<Student> stuset;
        stuset.insert(Student("zhangsan",22));  //默认为升序 会自动调用<操作符重载
        stuset.insert(Student("lisi",52));
        stuset.insert(Student("wangwu",48));
                              //插入
        vector<Student>stuvec;
        stuvec.push_back(Student("lisi",22));
        stuvec.push_back(Student("xiaoli",22));
        stuvec.push_back(Student("xiaowu",22));
    
        stuset.insert(stuvec.begin(),stuvec.end()); //插入另一个容器的范围
        pair<set<Student>::iterator,bool> pr = stuset.insert(Student("yixiaop",22));
        if(pr.second)
        {
            cout<<"insert"<<endl;
            pr.first->showStu();
        }
        else
            cout<<"not insert"<<endl;
        for_each(stuset.begin(),stuset.end(),show);
    }
        int i= stuset.count(Student("zhangsan",22));//判断值是否存在
        if(i>0)
        {
            cout<<"have the value"<<endl;
        }
        else
            cout<<"not have the value"<<endl;
                                                查找与删除
    
    //    int n = stuset.erase(Student("zhangsan",0));
    //    if(n>0)
    //        cout<<"erase ok"<<endl;
    //    else
    //        cout<<"erase error"<<endl;        
    //     for_each(stuset.begin(),stuset.end(),show);
    //     cout<<"__________________"<<endl;
    //    stuset.erase(++stuset.begin(),stuset.end());
         stuset.erase(stuset.begin());
         for_each(stuset.begin(),stuset.end(),show);
    }
    #endif
    //set的容器特性:
    //1.底层实现(二叉树,链表),增删效率低,查询效率非常高
    //2.有序容器(自动排序)
    //3.不允许有重复值
    //4.自己有find(),erase(),不需要
  • 相关阅读:
    Python 元组
    Python 字典
    Python 列表
    Python 数字(函数)
    Python 序列操作符与函数(字符串)
    JavaScript使用IEEE 标准进行二进制浮点运算,产生莫名错误
    网站用户体验要点(翻译理论)
    去掉checkbox边框的方法
    WPF数据模板样式选择器
    JS中年份问题的纠结!
  • 原文地址:https://www.cnblogs.com/xiaozoui11cl/p/12788279.html
Copyright © 2011-2022 走看看