加文

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

list unique

#include <iostream>
#include <list>
#include <algorithm>
#include <iomanip>
#include <string>
using namespace std;

template <class T>
class Member
{
    public:
        Member(T f, T l) :
            first_n(f), last_n(l) {}
        void print();
    private:
        string last_n, first_n;
    // for sort function
    friend bool operator < (Member& m1,
            Member& m2)
    { return m1.last_n < m2.last_n; }

    // for merge and unique functions 
    friend bool operator == (Member& m1,
            Member& m2)
    { return m1.last_n == m2.last_n; }
};
//---------------------------------------
template <class T>
void Member<T>::print()
{
    cout.setf(ios::left);
    cout << setw(15) << last_n.c_str()
         << first_n << endl;
}

typedef Member<string> M;
//========================================
int main () 
{
    list<M> li1;
    li1.push_back(M("Linda","Smith"));
    li1.push_back(M("Robert","Frost"));
    li1.push_back(M("Alex","Amstrong"));

    list<M> li2;
    li2.push_back(M("Linda","Smith"));
    li2.push_back(M("John","Wood"));
    li2.push_back(M("Alex","Amstrong"));

    li1.sort();
    li2.sort();
    li1.merge(li2);
    
    cout << "li1 after sorting and mergin"
         << endl;

    list<M>::iterator It = li1.begin();
    while ( It != li1.end() )
    {
        (It++)->print();
    }
    cout << endl;
    
    li1.unique();

    cout << "After li1.unique()" << endl;

    It = li1.begin();
    while ( It != li1.end() )
    {
        (It++)->print();
    }
    cout << endl;
    getchar();
    return 0;
}

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


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