Advertisement
Guest User

Untitled

a guest
Dec 21st, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream> // std::cout
  2. #include <algorithm> // std::sort
  3. #include <vector> // std::vector
  4.  
  5. int getYear(int date) {
  6. int year = (date / 10000);
  7.  
  8. return (year < 20) ? 2000+year : year;
  9.  
  10. }
  11. int getMonth(int date) {
  12.  
  13. int month = (date / 100) % 100;
  14. return (date /100) % 100;
  15.  
  16.  
  17. }
  18. int getDay(int date) {
  19. int d2 = (date /10) % 10;
  20. int d1 = (date /100) % 10;
  21.  
  22. return d2*10+d1;
  23.  
  24. }
  25. bool myfunction (int i,int j) {
  26.  
  27.  
  28. if (getYear(i) > getYear(j)) return true;
  29. if (getYear(i) == getYear(j) && getMonth(i) > getMonth(j)) return true;
  30. if (getYear(i) == getYear(j) && getMonth(i) == getMonth(j) && getDay(i) > getDay(j)) return true;
  31. return false;
  32. }
  33.  
  34. int main () {
  35. int myints[] = {130525,951022 , 130624, 121212};
  36.  
  37.  
  38.  
  39. std::vector<int> myvector (myints, myints+4); // 32 71 12 45 26 80 53 33
  40.  
  41. std::sort (myvector.begin(), myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
  42.  
  43.  
  44. std::cout << "Year: " << getYear(130525);
  45. std::cout << "Month:" << getMonth(130525);
  46. std::cout << "Day:" << getDay(130525);
  47.  
  48. std::cout << std::endl <<std::endl;
  49. std::cout << "Year: " << getYear(951022);
  50. std::cout << "Month:" << getMonth(951022);
  51. std::cout << "Day:" << getDay(951022);
  52.  
  53. std::cout << std::endl;
  54.  
  55. std::cout << "myvector contains:";
  56. for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
  57. std::cout << ' ' << *it;
  58. std::cout << '\n';
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement