GastonFontenla

Funcion comparadora para el sort

May 20th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <time.h>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. struct Par
  10. {
  11. int first, second;
  12. };
  13.  
  14. bool comparar(const Par &a, const Par&b)
  15. {
  16. if(a.first < b.first)
  17. return true;
  18. if(a.first > b.first)
  19. return false;
  20. if(a.second < b.second)
  21. return true;
  22. return false;
  23. }
  24.  
  25. Par constructor(int a, int b)
  26. {
  27. Par myPar;
  28. myPar.first = a;
  29. myPar.second = b;
  30. return myPar;
  31. }
  32.  
  33. int main()
  34. {
  35. srand(time(0));
  36. vector <Par> myVec;
  37.  
  38. for(int i=0; i<10; i++)
  39. {
  40. myVec.push_back(constructor(rand()%10, rand()%20));
  41. }
  42.  
  43. for(int i=0; i<myVec.size(); i++)
  44. {
  45. cout << myVec[i].first << " " << myVec[i].second << endl;
  46. }
  47.  
  48. sort(myVec.begin(), myVec.end(), comparar);
  49.  
  50. cout << "ORDENADO: " << endl;
  51. for(int i=0; i<myVec.size(); i++)
  52. {
  53. cout << myVec[i].first << " " << myVec[i].second << endl;
  54. }
  55.  
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment