Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. struct Students
  2. {
  3. string name;
  4. string surname;
  5. int test1;
  6. };
  7. Students student[100];
  8.  
  9. #include <iostream>
  10. #include <algorithm>
  11.  
  12. typedef struct {
  13. std::string name;
  14. std::string surname;
  15. int test;
  16. } StudentsType;
  17.  
  18. int main () {
  19. StudentsType student[3] = {
  20. {name: "Иван", surname: "Иванов", test: 3},
  21. {name: "Петр", surname: "Петров", test: 2},
  22. {name: "Степан", surname: "Степанов", test: 4}
  23. };
  24. std::cout << "nДо сортировки:" << std::endl;
  25. for(const auto &i:student) std::cout << i.surname << " " << i.name << " : " << i.test << std::endl;
  26. std::sort(std::begin(student),std::end(student),[&](StudentsType &one, StudentsType &two) {
  27. return one.test>two.test;
  28. });
  29. std::cout << "nПосле сортировки:" << std::endl;
  30. for(const auto &i:student) std::cout << i.surname << " " << i.name << " : " << i.test << std::endl;
  31. return 0;
  32. }
  33.  
  34. До сортировки:
  35. Иванов Иван : 3
  36. Петров Петр : 2
  37. Степанов Степан : 4
  38.  
  39. После сортировки:
  40. Степанов Степан : 4
  41. Иванов Иван : 3
  42. Петров Петр : 2
  43.  
  44. #include <vector>
  45. #include <string>
  46. #include <iostream>
  47. #include <iomanip>
  48. #include <algorithm>
  49.  
  50. using namespace std;
  51.  
  52. struct Data
  53. {
  54. int field;
  55. char bigData[20];
  56. Data(int x):field(x){};
  57. };
  58.  
  59.  
  60. int main(int argc, const char * argv[])
  61. {
  62. // Первый вариант
  63. vector<Data> d;
  64. for(int i = 0; i < 20; ++i) d.push_back(Data(rand()%30));
  65. sort(d.begin(),d.end(),
  66. [](const Data& d1, const Data& d2) { return d1.field < d2.field; });
  67. for(auto e: d)
  68. cout << e.field << endl;
  69.  
  70. cout << "-----------8<-----------n";
  71.  
  72. // Второй вариант
  73. vector<Data*> p;
  74. for(int i = 0; i < 20; ++i) p.push_back(new Data(rand()%30));
  75. sort(p.begin(),p.end(),
  76. [](const Data* d1, const Data* d2) { return d1->field < d2->field; });
  77. for(auto e: p)
  78. cout << e->field << endl;
  79.  
  80.  
  81. }
  82.  
  83. Students **p;
  84. p = new Students*[col];
  85. for (int i = 0; i < col; i++)
  86. {
  87. p[i] = &student[i];
  88. }
  89.  
  90. for (int i = 0; i < col - 1; i++)
  91. {
  92. for (int j = 0; j < col - i - 1; j++)
  93. {
  94. if (p[j]->test1 + p[j]->test2 + p[j]->test3 < p[j+1]->test1 + p[j+1]->test2 + p[j+1]->test3)
  95. {
  96. Students *temp = p[j];
  97. p[j] = p[j + 1];
  98. p[j + 1] = temp;
  99. }
  100. }
  101. }
  102.  
  103. #include <iostream>
  104. #include <iterator>
  105. #include <algorithm>
  106.  
  107. struct Student
  108. {
  109. std::string name;
  110. std::string surname;
  111. int test1;
  112. };
  113.  
  114. struct SortStudents
  115. {
  116. static bool byName(const Student *first, const Student *second)
  117. {
  118. return first->name < second->name;
  119. }
  120.  
  121. static bool bySurname(const Student *first, const Student *second)
  122. {
  123. return first->surname < second->surname;
  124. }
  125.  
  126. static bool byTest(const Student *first, const Student *second)
  127. {
  128. return first->test1 < second->test1;
  129. }
  130. };
  131.  
  132. std::ostream & operator <<(std::ostream &os, Student &st)
  133. {
  134. return os << st.name << ' ' << st.surname << ' ' << st.test1;
  135. }
  136.  
  137. int main()
  138. {
  139. Student students[] =
  140. {
  141. { "A", "YY", 10 },
  142. { "C", "ZZ", 6 },
  143. { "B", "XX", 12 }
  144. };
  145.  
  146. const size_t N = sizeof(students) / sizeof(*students);
  147.  
  148. Student *pStudents[N];
  149.  
  150. for (size_t i = 0; i < N; i++) pStudents[i] = &students[i];
  151.  
  152. std::cout << "Sorted by name" << std::endl;
  153.  
  154. std::sort(std::begin(pStudents), std::end(pStudents),
  155. SortStudents::byName);
  156.  
  157. for (auto p : pStudents)
  158. {
  159. std::cout << *p << std::endl;
  160. }
  161.  
  162. std::cout << "nSorted by surname" << std::endl;
  163.  
  164. std::sort(std::begin(pStudents), std::end(pStudents),
  165. SortStudents::bySurname);
  166.  
  167. for (auto p : pStudents)
  168. {
  169. std::cout << *p << std::endl;
  170. }
  171.  
  172. std::cout << "nSorted by tests" << std::endl;
  173.  
  174. std::sort(std::begin(pStudents), std::end(pStudents),
  175. SortStudents::byTest);
  176.  
  177. for (auto p : pStudents)
  178. {
  179. std::cout << *p << std::endl;
  180. }
  181.  
  182. return 0;
  183. }
  184.  
  185. Sorted by name
  186. A YY 10
  187. B XX 12
  188. C ZZ 6
  189.  
  190. Sorted by surname
  191. B XX 12
  192. A YY 10
  193. C ZZ 6
  194.  
  195. Sorted by tests
  196. C ZZ 6
  197. A YY 10
  198. B XX 12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement