zoukankan      html  css  js  c++  java
  • set_map_二叉搜索树

    /*
    set:二叉搜索树维护集合的容器
    map:维护键和键对应的值的容器
    */
    #include<iostream>
    #include<set>
    #include<map>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        set<int> s;
    
        s.insert(1);
        s.insert(5);
        s.insert(3);
    
        set<int>::iterator ite;
    
        ite = s.find(1);
        if (ite == s.end())cout << "not found";
        else cout << "found";
    
        //删除元素
        s.erase(1);
    
        //其它查找方式
        if (s.count(3) != 0)cout << "found";
        else cout << "not found";
    
        //遍历
        for (ite = s.begin(); ite != s.end(); ite++)
            cout << *ite << " ";
    
        map<int, const char*> m;
    
        m.insert(make_pair(1, "one"));
        m.insert(make_pair(10, "ten"));
        m[100] = "hundred";
    
        map<int, const char*>::iterator ite2;
        ite2 = m.find(1);//查找
        cout << ite2->second << endl;
    
        ite2 = m.find(2);
        if (ite2 == m.end())cout << "not found";
        else cout << ite2->second;
    
        cout << m[10];//其它写法
    
        m.erase(10);//删除
    
        //遍历
        for (ite2 = m.begin(); ite2 != m.end(); ite2++)
            cout << ite2->first << " " << ite2->second << endl;
    
        system("pause");
        return 0;
    }

     

    /*
        set 结构体排序用法
        实例:用set查找人名
        strcpy(a,"name"):赋给字符串a值“name”
        strcmp(a,b):a>b=1,a<b=-1,a==b=0
    */
    #include<iostream>
    #include<cstdio>
    #include<set>
    #include<cstring>
    using namespace std;
    
    struct student{
        char name[10];
        int stunum;
    };
    
    //set 排序的规则
    class cmpp
    {
        public:
        bool operator()(const student &a, const student &b) const{
            if(strcmp(a.name,b.name)<0)return true;
            else if(strcmp(a.name,b.name)==0)return a.stunum<b.stunum;
            else return false;
        }
    };
    
    int main()
    {
        student a,b;
        strcpy(a.name,"赵钱");
        a.stunum=1;
        strcpy(b.name,"孙李");
        b.stunum=9;
        set<student,cmpp> s;
        s.insert(a);
        s.insert(b);
    
        set<student,cmpp>::iterator iter;
    
        for(iter=s.begin();iter!=s.end();iter++)cout<<(*iter).stunum<<endl;
    
        //查找
        iter=s.find(b);
    
        //iter若不等于s.end(),说明查找到目标(iter!=s.end()非常重要)
        if(iter!=s.end()){cout<<"name:";puts((*iter).name);cout<<endl;}
        else cout<<"not find
    ";
    
        return 0;
    }

    世上无难事,只要肯登攀。
  • 相关阅读:
    CSS Modules
    回调地狱
    css实现双色饼图
    vue项目中使用less
    pug(jade) 学习笔记
    React组件proptypes, ref
    react+express实现跨域
    react高阶组件
    Oracle数据库出现锁表情况分析
    JPA常用注解记录
  • 原文地址:https://www.cnblogs.com/littlehoom/p/3513921.html
Copyright © 2011-2022 走看看