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;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章