list-remove

////////////////////////////////////////
//      2018/04/26 14:20:48
//      list-remove

// removes elements from the list
#include <iostream>
#include <list>
#include <algorithm>
#include <string>

using namespace std;

template<class T, class D>
class Salary
{
private:
    T id;
    D sal;
public:
    Salary(T t) :id(t){}
    Salary(T t, D d) : id(t), sal(d){}

    void print(){
        cout << id << " " << sal << endl;
    }

    friend bool operator ==(const Salary& s1, const Salary& s2){
        return s1.id == s2.id;
    }
};
//=====================================
int main(){
    typedef Salary<string, double> S;
    typedef list<S> L;

    L l;
    l.push_back(S("012345",70000.0));
    l.push_back(S("012346",60000.0));
    l.push_back(S("012347",72000.0));

    L::iterator it = l.begin();
    while (it != l.end()){
        (it++)->print();
    }
    cout << endl;

    S s("012345");
    l.remove(s);

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

/*
OUTPUT:
    012345 70000
    012346 60000
    012347 72000

    012346 60000
    012347 72000
*/ 
原文地址:https://www.cnblogs.com/laohaozi/p/12537943.html