Advertisement
Shokedbrain

courses

Jun 7th, 2021
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using std::cin;
  7. using std::cout;
  8. using std::endl;
  9. using std::vector;
  10. using std::string;
  11.  
  12. struct course
  13. {
  14.     int id = 0;
  15.     vector<string> students{};
  16. };
  17.  
  18. void fill_in(course* c)
  19. {
  20.     // заполняем структуру
  21.     cout << "enter id: ";
  22.     cin >> c->id;
  23.     int count;
  24.     cout << "enter count of students: ";
  25.     cin >> count;
  26.     for (int i = 0; i < count; i++)
  27.     {
  28.         string buf{};
  29.         cout << "enter name of student: ";
  30.         cin >> buf;
  31.         c->students.push_back(buf);
  32.     }
  33. }
  34.  
  35. void show(const course& c)
  36. {
  37.     cout << "id: " << c.id << endl;
  38.     cout << "students" << endl;
  39.     for (string a: c.students)
  40.         cout << a << endl;
  41. }
  42.  
  43. int count(const course c)
  44. {
  45.     return c.students.size();
  46. }
  47.  
  48. bool compare(const course &first, const course &second)
  49. {
  50.     // для убывания
  51.     return first.students.size() > second.students.size();
  52. }
  53.  
  54. int main()
  55. {
  56.     int numb = 0;
  57.     cout << "enter number of courses: ";
  58.     cin >> numb;
  59.     vector<course> courses;
  60.     for (int i = 0; i < numb; i++)
  61.     {
  62.         course *c = new course;
  63.         fill_in(c);
  64.         courses.push_back(*c);
  65.     }
  66.     cout << courses.size() << endl;
  67.     std::sort(courses.begin() ,courses.end(), compare);
  68.     for (course a: courses)
  69.         show(a);
  70.     system("pause");
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement