https://www.cnblogs.com/tianyaju ... /05/24/2516881.html
使用vector的sort方法,有两种形式,一种是在类内部使用操作符重载实现,一种是在类外面写一个比较函数。但是sort方法无法直接传入其它形参,在需要动态排序相对某个人的位置时,采取了间接通过静态变量的方法。为防止并发量巨大时,静态变量无法及时改变而产生错误数据,故用一循环去验证等待赋值,排完序后再还原为初始值。 - #include <iostream>
- #include <string>
- #include <cmath>
- #include <vector>
- using namespace std;
- double calc_distance(double _long1,double _lat1,double _long2,double _lat2)
- {
- double rad = 6371.0;
- const double PI = 3.14159265358979323846;
- double long1 = _long1/180.0*PI;
- double lat1 = _lat1/180.0*PI;
- double long2 = _long2/180.0*PI;
- double lat2 = _lat2/180.0*PI;
- return rad*(acos(cos(lat2)*cos(lat1)*cos(long2-long1)+sin(lat2)*sin(lat1)));
- }
- class User
- {
- private:
- int id;
- double longitude;
- double latitude;
- public:
- int getid(){return id;}
- double getlongitude(){return longitude;}
- double getlatitude(){return latitude;}
- User(int _id,double _longitude,double _latitude)
- {
- id = _id;
- longitude = _longitude;
- latitude = _latitude;
- }
- bool operator<(const User& obj) const
- {
- return id < obj.id;
- }
- bool operator>(const User& obj) const
- {
- return id > obj.id;
- }
- friend bool sort_by_distance(const User& obj1,const User& obj2);
- friend bool sort_by_id(const User& obj1,const User& obj2);
- };
- static double _longitude = 0.0;
- static double _latitude = 0.0;
- bool sort_by_distance(const User& obj1,const User& obj2)
- {
- return calc_distance(obj1.longitude,obj1.latitude,_longitude,_latitude)< calc_distance(obj2.longitude,obj2.latitude,_longitude,_latitude);
- }
- bool sort_by_id(const User& obj1,const User& obj2)
- {
- return obj1.id < obj2.id;
- }
- int main()
- {
- vector<User> vect;
- User test1(3,20.1,20.1);
- User test2(2,30.1,30.1);
- User test3(1,40.1,40.1);
- User test4(4,50.1,50.1);
- User test5(5,60.1,60.1);
- vect.push_back(test1);
- vect.push_back(test2);
- vect.push_back(test3);
- vect.push_back(test4);
- vect.push_back(test5);
- while(true)
- {
- if(_longitude == 0.0 && _latitude == 0.0)
- {
- _longitude = 30.1;
- _latitude = 30.1;
- break;
- }
- }
- sort(vect.begin(),vect.end(),sort_by_distance);
- //sort(vect.begin(),vect.end(),sort_by_id);
- //sort(vect.begin(),vect.end(),less<User>());
- //sort(vect.begin(),vect.end(),greater<User>());
- _longitude = 0.0;
- _latitude = 0.0;
- vector<User>::iterator it = vect.begin();
- while(it != vect.end())
- {
- cout<<"ID:"<<it->getid()<<"\tLong:"<<it->getlongitude()<<"\tLat:"<<it->getlatitude()<<endl;
- it++;
- }
- }
复制代码
|