• set-find


    ////////////////////////////////////////
    //      2018/04/28 18:53:08
    //      set-find
    
    // find a given element
    #include <iostream>
    #include <set>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    template<class T>
    class Member{
    private:
        T first, last;
    public:
        Member(T l, T f) :first(f), last(l){}
        void print() const{
            cout.setf(ios::left);
            cout << setw(15) << first << " " << last << endl;
        }
    
            friend bool operator <(const Member& m1, const Member& m2){
            return m1.last < m2.last;
        }
        friend bool operator == (const Member& m1, const Member& m2){
            return m1.last == m2.last;
        }
    };
    //======================
    int main(){
    
        typedef Member<string> M;
        typedef set<M, less<M>> S;
        M m("Forst", "Robert");
        S s;
    
        s.insert(m);
        s.insert(M("Smith", "John"));
        s.insert(M("Amstrong", "Bill"));
        s.insert(M("Bain", "Linda"));
    
        S::iterator it = s.begin();
        while (it != s.end()){
            (it++)->print();
        }
    
        it = s.find(m);
        if (it == s.end()){
            cout << "element is not found." << endl;
        }
        else{
            cout << "element is found:";
            (*it).print();
        }
    
        return 0;
    };
    
    /*
    OUTPUT:
        Bill            Amstrong
        Linda           Bain
        Robert          Forst
        John            Smith
        element is found:Robert          Forst
    */ 
  • 相关阅读:
    数据库之事务与常见故障
    数学的魅力 之 正多边形
    html5 的基础理解1
    android 引入开源项目
    android 图片查看器
    java 线程安全
    python3 自动生成requirement.txt
    centos 7 安装 python3.7
    python3 创建,激活虚拟环境
    Mac 配置poetry
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537908.html
Copyright © 2020-2023  润新知