রবিবার, ২৭ সেপ্টেম্বর, ২০১৫

Sorting vector of structs

If you have some complex form of data and need to sort them according to some rules.


 #include <bits/stdc++.h>  
 using namespace std;  
 //A list of people with names,age,position  
 //sort them according to their ages( higher to lower ) names (lexicographical order [higher to lower])  
 //distance from origin (lower to higher)  
 //else on their relative position (lower to higher)//based on when the data is inserted  
 struct people  
 {  
   string name;  
   int age;  
   pair<int,int>pos;  
   int r_pos;  
   people(string nm,int ag,pair<int,int>ps,int r_pos)  
   {  
     this->name = nm;  
     this->age = ag;  
     pos.first = ps.first;  
     pos.second = ps.second;  
     this->r_pos = r_pos;  
   }  
 };  
 bool compare(people i,people j)  
 {  
   double dist_i = sqrt(i.pos.first*i.pos.first + i.pos.second*i.pos.second) ;  
   double dist_j = sqrt(j.pos.first*j.pos.first + j.pos.second*j.pos.second) ;  
   if(i.age!=j.age)  
     return i.age > j.age ; //previous element's age > next element's age in the final vector  
   if(i.name != j.name)  
     return i.name > j.name ; //previous element's name > next element's name in the final vector  
   if(dist_i!=dist_j) //not using EPS  
     return dist_i < dist_j ; //previous element's distance < next element's distance in the final vector (from origin )  
   return i.r_pos < j.r_pos ; //previous element's rel_position < next element's position in the final vector  
 }  
 int main()  
 {  
   vector<people>ppl;  
   people pip("Nabil",18,make_pair(3,4),1);  
   ppl.push_back(pip);  
   pip.r_pos = 2;  
   ppl.push_back(pip);  
   cout<<ppl.size()<<endl;  
 //Modification  
   pip.name = "Neo";  
   pip.age = 21;  
   pip.r_pos = 3;  
   ppl.push_back(pip);  
   pip.name = "Zan";  
   pip.pos = make_pair(5,6);  
   pip.r_pos = 4;  
   ppl.push_back(pip);  
   for(vector<people>::iterator it = ppl.begin(); it!=ppl.end(); it++)  
   {  
     people temp = *it ;  
     cout<<"People "<<it - ppl.begin() + 1 <<endl;  
     cout<<temp.name<<endl;  
     cout<<temp.age<<endl;  
     cout<<temp.pos.first<<" "<<temp.pos.second<<endl;  
     cout<<temp.r_pos<<endl;  
   }  
   sort(ppl.begin(),ppl.end(),compare); //Don't pass compare()  
   cout<<"-------------------------------------"<<endl;  
   for(vector<people>::iterator it = ppl.begin(); it!=ppl.end(); it++)  
   {  
     people temp = *it ;  
     cout<<"People "<<it - ppl.begin() + 1 <<endl;  
     cout<<temp.name<<endl;  
     cout<<temp.age<<endl;  
     cout<<temp.pos.first<<" "<<temp.pos.second<<endl;  
     cout<<temp.r_pos<<endl;  
   }  
 //people lister  
   cout<<"++++++++++++++++++++++++++++++++++++++"<<endl;  
   int n = 0;  
   cin>>n;  
   cout<<"Name Age pos_x pos_y\n";  
   ppl.clear();//clearing before new listing  
   for(int a=1; a<=n; a++)  
   {  
     string name;  
     int age;  
     int pos_x,pos_y;  
     cin>>name>>age>>pos_x>>pos_y;  
     pip.name = name;  
     pip.age = age ;  
     pip.pos.first = pos_x;  
     pip.pos.second = pos_y;  
     pip.r_pos = a;  
     ppl.push_back(pip);  
   }  
   sort(ppl.begin(),ppl.end(),compare); //Don't pass compare()  
   cout<<"-------------------------------------"<<endl;  
   for(vector<people>::iterator it = ppl.begin(); it!=ppl.end(); it++)  
   {  
     people temp = *it ;  
     cout<<"People "<<it - ppl.begin() + 1 <<endl;  
     cout<<temp.name<<endl;  
     cout<<temp.age<<endl;  
     cout<<temp.pos.first<<" "<<temp.pos.second<<endl;  
     cout<<temp.r_pos<<endl;  
   }  
   return 0;  
 }