zoukankan      html  css  js  c++  java
  • vector 排序方法sort的使用

    使用vector的sort方法,有两种形式,一种是在类内部使用操作符重载实现,一种是在类外面写一个比较函数。但是sort方法无法直接传入其它形参,在需要动态排序相对某个人的位置时,采取了间接通过静态变量的方法。为防止并发量巨大时,静态变量无法及时改变而产生错误数据,故用一循环去验证等待赋值,排完序后再还原为初始值。

      1 #include <iostream>
      2 #include <string>
      3 #include <cmath>
      4 #include <vector>
      5 
      6 using namespace std;
      7 
      8 double calc_distance(double _long1,double _lat1,double _long2,double _lat2)
      9 {
     10     double rad = 6371.0;
     11     const double PI = 3.14159265358979323846;
     12     double long1 = _long1/180.0*PI;
     13     double lat1 = _lat1/180.0*PI;
     14     double long2 = _long2/180.0*PI;
     15     double lat2 = _lat2/180.0*PI;
     16     return rad*(acos(cos(lat2)*cos(lat1)*cos(long2-long1)+sin(lat2)*sin(lat1)));
     17 }
     18 class User
     19 {
     20     private:
     21         int id;
     22         double longitude;
     23         double latitude;
     24 
     25     public:
     26         int getid(){return id;}
     27         double getlongitude(){return longitude;}
     28         double getlatitude(){return latitude;}
     29     User(int _id,double _longitude,double _latitude)
     30     {
     31         id = _id;
     32         longitude = _longitude;
     33         latitude = _latitude;
     34     }
     35     
     36     bool operator<(const User& obj) const
     37     {
     38         return id < obj.id;
     39     }
     40 
     41     bool operator>(const User& obj) const
     42     {
     43         return id > obj.id;
     44     }
     45     
     46     friend bool sort_by_distance(const User& obj1,const User& obj2);
     47     friend bool sort_by_id(const User& obj1,const User& obj2);
     48 };
     49 
     50 static double _longitude = 0.0;
     51 static double _latitude = 0.0;
     52 
     53 bool sort_by_distance(const User& obj1,const User& obj2) 
     54 {
     55     return calc_distance(obj1.longitude,obj1.latitude,_longitude,_latitude)< calc_distance(obj2.longitude,obj2.latitude,_longitude,_latitude);
     56 }
     57 
     58 bool sort_by_id(const User& obj1,const User& obj2)
     59 {
     60     return obj1.id < obj2.id;
     61 }
     62 int main()
     63 {
     64     vector<User> vect;
     65     User test1(3,20.1,20.1); 
     66     User test2(2,30.1,30.1);
     67     User test3(1,40.1,40.1);
     68     User test4(4,50.1,50.1);
     69     User test5(5,60.1,60.1);
     70     vect.push_back(test1);
     71     vect.push_back(test2);
     72     vect.push_back(test3);
     73     vect.push_back(test4);
     74     vect.push_back(test5);
     75     while(true)
     76     {
     77         if(_longitude == 0.0 && _latitude == 0.0)
     78         {
     79             _longitude = 30.1;
     80             _latitude = 30.1;
     81             break;
     82         }
     83     }
     84     
     85     sort(vect.begin(),vect.end(),sort_by_distance);
     86     //sort(vect.begin(),vect.end(),sort_by_id);
     87     //sort(vect.begin(),vect.end(),less<User>());
     88     //sort(vect.begin(),vect.end(),greater<User>());
     89 
     90     _longitude = 0.0;
     91     _latitude = 0.0;
     92 
     93     vector<User>::iterator it = vect.begin();
     94     while(it != vect.end())
     95     {
     96         cout<<"ID:"<<it->getid()<<"\tLong:"<<it->getlongitude()<<"\tLat:"<<it->getlatitude()<<endl;
     97         it++;
     98     }
     99 
    100 }
  • 相关阅读:
    BZOJ3171: [Tjoi2013]循环格
    Luogu P1850 换教室(期望dp)
    Luogu P3825 [NOI2017]游戏(2-SAT)
    Luogu P3007 [USACO11JAN]大陆议会The Continental Cowngress
    Luogu P2272 [ZJOI2007]最大半连通子图(Tarjan+dp)
    Luogu P3209 [HNOI2010]平面图判定(2-SAT)
    Luogu P4171 [JSOI2010]满汉全席(2-SAT)
    Luogu P4782 【模板】2-SAT 问题(2-SAT)
    Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)
    Luogu P4933 大师(dp)
  • 原文地址:https://www.cnblogs.com/tianyajuanke/p/2516881.html
Copyright © 2011-2022 走看看