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(),不需要
  • 相关阅读:
    C#学习记录二:高级数据存储方式
    SharePoint 2010 匿名用户调用Client Object Model访问列表项
    Android 在闹钟开机时,如何解决开机动画没有播完就进入Launcher M
    Getting in Line UVA 216
    Android 如何关闭Navigation Bar M
    google protocol buffer 简介 版本 安装 使用 实例
    Android [VP]视频播放器播放本地视频时收到短信/彩信,需要界面提示 M
    Maven教程初级篇02:pom.xml配置初步
    当Ruby的model名字出错时,在现实view时显示错误的提示
    VS Code 安装 C++ 调试环境
  • 原文地址:https://www.cnblogs.com/xiaozoui11cl/p/12788279.html
Copyright © 2011-2022 走看看