关于 set/map
  • 板块学术版
  • 楼主Piwry
  • 当前回复3
  • 已保存回复3
  • 发布时间2020/10/29 19:13
  • 上次更新2023/11/5 09:34:35
查看原帖
关于 set/map
105254
Piwry楼主2020/10/29 19:13
const double eps =1e-6;

inline bool eq(const double &x, const double &y){
	return (x-y < eps && x-y > -eps);
}

inline bool less(const double &x, const double &y){
	return (x-y <= -eps);
}

inline double Abs(const double &x){ return (x < 0) ? -x : x; }

struct Pool{
	double Weight;
	int id;
	
	Pool():Weight(0), id(0){}
	Pool(const double &WEIGHT, const int &ID):Weight(WEIGHT), id(ID){}
	bool operator < (const Pool &B) const{
		if(eq(Weight, B.Weight))
			return id > B.id;
		else
			return less(Weight, B.Weight);
		/*或这样写,最后结果仍一样 
		if(Abs(Weight-B.Weight)/1.0*max(1.0, B.Weight) < 1e-6)
			return id < B.id;
		else
			return Weight < B.Weight;
		*/
	}
};

set<Pool> Set1;

int main(){
	/*...*/
	
	if(!Set1.empty()){
		set<Pool>::iterator it =Set1.end();
		--it;
		Pool tmp =*it;
		if(Set1.erase(tmp) == 0 && Set1.count(tmp) == 0)
			puts("Signed1");
	}
	
	/*...*/
}

在某些数据下,程序输出含 Signed1

c++11,可以确保其它部分没有发生越界或其它错误

想问下原因qaq

2020/10/29 19:13
加载中...