1. 程式人生 > >std::set進行排序並刪除重複資料

std::set進行排序並刪除重複資料

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

struct  A
{
    string ssName;
    string ssSex;
    int nAge;
};

class ASortByName
{
public:
    bool operator()(const A& a,const A& b)
    {
        if (a.ssName.compare(b.ssName) < 0)
        {
            return
true; } else if (a.ssName.compare(b.ssName)==0 && a.ssSex.compare(b.ssSex)<0) { return true; } else { return false; } } }; struct Test { static const auto i = 0; int j = 1; }; int main(int argc, char
* argv[]) { A a = {"wanglj","man",30}; A b = {"liuting","femail",30}; A c = {"wanglj","fan",30}; set<A,ASortByName> setA; setA.insert(a); setA.insert(b); setA.insert(c); for (auto iter = setA.begin(); iter != setA.end(); ++iter) { cout << iter->ssName << "\t"
<< iter->ssSex << "\t" << iter->nAge << endl; } }