加文

希望是美好的……
随笔 - 0, 文章 - 209, 评论 - 0, 引用 - 0
数据加载中……

set end

#include <iostream>
#include <set>
#include <iomanip>
#include <string>
using namespace std;

template <class T>
class Member
{
    public:
        Member(T l, T f) : last(l), first(f) {}
        void print() const // const !!!
        {
            cout.setf(ios::left);
            cout << setw(15) << first.c_str()
                 << last << endl;
        }
    private:
        T first, last;
    // const !!!
    friend bool operator < (const Member& m1, const Member& m2)
    {
        return (m1.last < m2.last) ? true : false;
    }
    friend bool operator == (const Member& m1, const Member& m2)
    {
        return (m1.last == m2.last) ? true : false;
    }
};
//===============================================
int main () 
{
    typedef Member<string> M;
    typedef set<M, less<M> > S;
    M m("Frost","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();
    getchar();
    return 0;
}

posted on 2012-04-12 21:40 加文 阅读(118) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理