c++ unique,lower_bound(运算符重载)

#include<bits/stdc++.h>
#define PI acos(-1)
#define ios ios::sync_with_stdio(false)
#define endl '\n'
#define srand srand((unsigned)time(NULL))
using namespace std;
typedef long long int ll;
typedef pair<int,int> PII;
const int maxn=1e5+10;
struct node{
	int x,y,z;
	node(int fir,int sec,int thi){
		x=fir;y=sec;z=thi;
	}
	node(){}
	node operator- () {  //重载负运算符 
        x = -x;
        y = -y;
        z = -z;
        return node(x,y,z);
    }
    bool operator <(const node& d)const{ // 重载小于运算符( < )
        if(x != d.x){
        	return x<d.x;
        }
        if(x == d.x && y != d.y){
        	return y<d.y;
        }
        if(y==d.y)return z<d.z;
    }
    bool operator >(const node& d)const{ // 重载大于运算符( > )
        if(x != d.x){
        	return x>d.x;
        }
        if(x == d.x && y != d.y){
        	return y>d.y;
        }
        if(y==d.y)return z>d.z;
    }
    
    bool operator ==(const node& d)const{// 重载等于运算符 
    	if(x==d.x&&y==d.y&&z==d.z)return true;
    	return false;
	}
	
	friend ostream &operator<<(ostream &output,const node &D){ //重载输出运算符 
        output << "first: " << D.x << " second : " << D.y<<" third: "<<D.z;
        return output;            
    }
 
    friend istream &operator>>( istream  &input,node &D){ //重载输入运算符 
        input >> D.x >> D.y>>D.z;
        return input;            
    }
}; 
int main(){
	srand;
    int n;cin>>n;
    node k;k.x=rand();k.y=rand();k.z=rand();
    node a[maxn];
    for(int i=1;i<=n;i++){
    	a[i].x=rand();
    	a[i].y=rand();
    	a[i].z=rand();
	}
	
    sort(a+1,a+n+1);
    //对数组 a 去重 
    int m=unique(a+1,a+1+n)-(a+1);
    
    for(int i=1;i<=m;i++){
    	cout<<a[i];cout<<endl;
	}
	
    //算法返回一个非递减序列[first, last)中的第一个大于等于值k的位置
    int it1=lower_bound(a+1,a+1+m,k)-a;
    
    
    //算法返回一个非递减序列[first, last)中第一个大于k的位置。
    int it2=upper_bound(a+1,a+m+1,k)-a;
    return 0;
}


全部评论

相关推荐

完美的潜伏者许愿简历...:隐藏信息被你提取出来了,暗示,这就是暗示
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-11 13:34
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务