Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. #include <vector>
  5. #include <sstream>
  6. #include <map>
  7. #include <iterator>
  8. #include <unordered_set>
  9. using namespace std;
  10.  
  11.  
  12.  
  13. static bool customSort(string str1, string str2) {
  14.  
  15.  
  16. int pos1 = 0, pos2 = 0;
  17. string tempStr1 = "", tempStr2 = "";
  18.  
  19. pos1 = str1.find(' ');
  20. tempStr1 = str1.substr(0, pos1);
  21. str1 = str1.substr(pos1+1, str1.size()-pos1-1);
  22.  
  23.  
  24. pos2 = str2.find(' ');
  25. tempStr2 = str2.substr(0, pos2);
  26. str2 = str2.substr(pos2+1, str2.size()-pos2-1);
  27.  
  28.  
  29. int i=0, j=0;
  30. while((i < str1.size()) && (j < str2.size())) {
  31. if(tolower(str1[i]) != tolower(str2[j]))
  32. return tolower(str1[i]) < tolower(str2[j]);
  33. i++;
  34. j++;
  35. }
  36.  
  37. if(i != str1.size())
  38. return 0;
  39.  
  40. if(j != str2.size())
  41. return 1;
  42.  
  43.  
  44. i = 0;
  45. j = 0;
  46.  
  47. while((i < tempStr1.size()) && (j < tempStr2.size())) {
  48. if ((tempStr1[i]-'0' >= 0) && (tempStr1[i]-'0' <= 9)) {
  49. if ((tempStr2[j]-'0' >= 0) && (tempStr2[j]-'0' <= 9)) {
  50. if (tempStr1[i]-'0' != tempStr2[j]-'0')
  51. return tempStr1[i]-'0' < tempStr2[j]-'0';
  52. } else {
  53. return 1;
  54. }
  55. } else {
  56. if ((tempStr2[j]-'0' >= 0) && (tempStr2[j]-'0' <= 9)) {
  57. return 0;
  58. } else {
  59. if(tolower(tempStr1[i]) != tolower(tempStr2[j]))
  60. return tolower(tempStr1[i]) < tolower(tempStr2[j]);
  61. }
  62. }
  63. i++;
  64. j++;
  65.  
  66. }
  67. return 1;
  68. }
  69.  
  70.  
  71. vector<string> reorderData(vector<string> myVec) {
  72. vector<string> res(myVec.size());
  73. vector<string> numRes;
  74. vector<string> alphRes;
  75.  
  76. int pos = 0;
  77. string str = "";
  78. for(int i=0; i<myVec.size(); i++) {
  79. str = myVec[i];
  80. pos = str.find(' ');
  81. str = str.substr(pos+1, str.size()-pos-1);
  82.  
  83. if((str[0]-'0' >= 0) && (str[0]-'0' <= 9))
  84. numRes.push_back(myVec[i]);
  85. else
  86. alphRes.push_back(myVec[i]);
  87. }
  88. sort(alphRes.begin(), alphRes.end(), customSort);
  89. copy(alphRes.begin(), alphRes.end(), res.begin());
  90.  
  91. //vector<string> ::reverse_iterator r_it = res.rbegin();
  92. copy(numRes.begin(), numRes.end(), res.rbegin());
  93. return res;
  94.  
  95. }
  96.  
  97.  
  98.  
  99. int main()
  100. {
  101. vector<string> vec = {"al 9 2 3 1",
  102. "g1 Act car",
  103. "zo4 4 7",
  104. "abl off KEY dog",
  105. "a8 act zoo",
  106. "g1 act Car ban"};
  107.  
  108.  
  109. vector<string> res = reorderData(vec);
  110. for(int i=0; i<res.size(); i++) {
  111. cout << res[i] << endl;
  112. }
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement