set-end

////////////////////////////////////////
//      2018/04/28 13:53:14
//      set-end

// returns an iterator to the last element
#include <iostream>
#include <set>
#include <iomanip>
#include <string>

using namespace std;

template<class T>
class Member{
private:
    T first, last;
public:
    Member(T f, T l) :first(f), last(l){}
    void print() const   // 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();
    }
    return 0;
}

/*
OUTPUT:
    Amstrong        Bill
    Smith           John
    Bain            Linda
    Forst           Robert
*/
原文地址:https://www.cnblogs.com/laohaozi/p/12537913.html