现在的位置: 首页 > 综合 > 正文

vector去重 nyoj 8 一种排序

2018年04月25日 ⁄ 综合 ⁄ 共 1192字 ⁄ 字号 评论关闭
        //去重
        vector<cuboid>::iterator end_unique=unique(a.begin(),a.end());
        //删除重复
        a.erase(end_unique,a.end());

本文来自http://hi.baidu.com/luckyboy  
博主的代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct cuboid
{
    int cid;
    int length;
    int width;
} box;
void print(cuboid s)
{
    cout<<s.cid<<" "<<s.length<<" "<<s.width<<endl;
}
bool operator < (const cuboid& lhs,const cuboid& rhs)
{
    if(lhs.cid>rhs.cid)
    {
        return false;//lhs.cid>rhs.cid;
    }
    else if(lhs.cid==rhs.cid&&lhs.length>rhs.length)
    {
        return false;//return lhs.length>rhs.length;
    }
    else if(lhs.cid==rhs.cid&&lhs.length==rhs.length&&lhs.width>rhs.width)
    {
        return false;//return lhs.width>rhs.width;
    }
    return true;
}
bool operator ==( const cuboid& lhs,const cuboid rhs )
{
    if(lhs.cid==rhs.cid&&lhs.length==rhs.length&&lhs.width==rhs.width)
    {
        return true;
    }
    else
        return false;
}
int main()
{
    vector<cuboid> a;
    int num1,num2;
    cin>>num1;
    for(int i=num1; i>0; i--)
    {
        cin>>num2;
        for(int j=num2; j>0; j--)
        {
            cin>>box.cid;
            cin>>box.length;
            cin>>box.width;
            if(box.length<box.width)
            {
                int t=box.length;
                box.length=box.width;
                box.width=t;
            }
            a.push_back(box);
        }
        sort(a.begin(),a.end());
        //去重
        vector<cuboid>::iterator end_unique=unique(a.begin(),a.end());
        //删除重复
        a.erase(end_unique,a.end());
        for_each(a.begin(),a.end(),print);
        a.clear();
    }
    return 0;
}

抱歉!评论已关闭.