zoukankan      html  css  js  c++  java
  • list-unique

    ////////////////////////////////////////
    //      2018/04/27 16:45:26
    //      list-unique
    
    // removes duplicate elements
    #include <iostream>
    #include <list>
    #include <algorithm>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    template<class T>
    class Member
    {
    private:
        T last_n, first_n;
    public:
        Member(T f, T l) :first_n(f), last_n(l){}
        void print();
    
        // for sort function
        friend bool operator < (Member& m1, Member& m2){
            return m1.last_n <  m2.last_n;
        }
    
        // for merge and unqiue 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 <<" "<< 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.sort();
        li1.unique();
    
        cout << "After li1.unique()" << endl;
    
        it = li1.begin();
        while (it != li1.end()){
            (it++)->print();
        }
        cout << endl;
        return 0;
    }
    
    /*
    OUTPUT:
        li1 after sorting and mergin
        Amstrong        Alex
        Amstrong        Alex
        Frost           Robert
        Smith           Linda
        Smith           Linda
        Wood            John
    
        After li1.unique()
        Amstrong        Alex
        Frost           Robert
        Smith           Linda
        Wood            John
    */ 
    
  • 相关阅读:
    pytorch报错:AttributeError: 'module' object has no attribute '_rebuild_tensor_v2'
    python运行报错:cannot import name 'InteractiveConsole'
    sudo pip3找不到命令
    pytorch入门1——简单的网络搭建
    caffe训练时报错
    python滴啊用caffe时的小坑
    求两个字符串的编辑距离
    归并排序
    复杂度n求数组的第K大值
    牛顿法与拟牛顿法学习笔记(一)牛顿法
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537926.html
Copyright © 2011-2022 走看看