Began to study the structure, and had the task where you need to sort the structure on a specific field. Used normal sorting, but it turns out to be irrational. Can anyone give an example of sorting a structure through pointers? How is this done? Suppose a structure is given with students and it must be sorted by the number of points for the test. How to do it most efficiently, if just sorting by field is considered irrational. It is necessary to use pointers, but how to organize it?

struct Students { string name; string surname; int test1; }; Students student[100]; 
  • pointer refers to some element in the structure. I don’t understand the question. Do you want to sort the structure by a special field in the structure? attach the structure to the question please? - Senior Pomidor
  • @Senoit Auomator, added structure - IWProgrammer
  • You have some kind of incomprehensible mixture of concepts. The concept of a field and the concept of a pointer are completely different things ... - Harry
  • @IWProgrammer The question is completely unclear. On the one hand, it is necessary to sort the structure by field, on the other hand, it is absolutely not rational. So what to do? - Vlad from Moscow

4 answers 4

Another option:

 #include <iostream> #include <algorithm> typedef struct { std::string name; std::string surname; int test; } StudentsType; int main () { StudentsType student[3] = { {name: "Иван", surname: "Иванов", test: 3}, {name: "Петр", surname: "Петров", test: 2}, {name: "Степан", surname: "Степанов", test: 4} }; std::cout << "\nДо сортировки:" << std::endl; for(const auto &i:student) std::cout << i.surname << " " << i.name << " : " << i.test << std::endl; std::sort(std::begin(student),std::end(student),[&](StudentsType &one, StudentsType &two) { return one.test>two.test; }); std::cout << "\nПосле сортировки:" << std::endl; for(const auto &i:student) std::cout << i.surname << " " << i.name << " : " << i.test << std::endl; return 0; } 

Conclusion:

 До сортировки: Иванов Иван : 3 Петров Петр : 2 Степанов Степан : 4 После сортировки: Степанов Степан : 4 Иванов Иван : 3 Петров Петр : 2 

Test for ideone .

    Not very yet clear. But - here are two options. The structures are sorted by field, in the second variant they are stored in a vector (or an array, it does not matter) as pointers.

    See, specify incomprehensible ...

     #include <vector> #include <string> #include <iostream> #include <iomanip> #include <algorithm> using namespace std; struct Data { int field; char bigData[20]; Data(int x):field(x){}; }; int main(int argc, const char * argv[]) { // Первый вариант vector<Data> d; for(int i = 0; i < 20; ++i) d.push_back(Data(rand()%30)); sort(d.begin(),d.end(), [](const Data& d1, const Data& d2) { return d1.field < d2.field; }); for(auto e: d) cout << e.field << endl; cout << "-----------8<-----------\n"; // Второй вариант vector<Data*> p; for(int i = 0; i < 20; ++i) p.push_back(new Data(rand()%30)); sort(p.begin(),p.end(), [](const Data* d1, const Data* d2) { return d1->field < d2->field; }); for(auto e: p) cout << e->field << endl; } 
    • well, I will clarify. Suppose you need to sort the structure. which was originally given. Here is the sorting through pointers, but it doesn't work. Students ** p; for (int i = 0; i <col - 1; i ++) {for (int j = 0; j <col - i - 1; j ++) {if (p [j] -> test1 + p [j] -> test2 + p [j] -> test3 <p [j + 1] -> test1 + p [j + 1] -> test2 + p [j + 1] -> test3) {Students * temp = p [j]; p [j] = p [j + 1]; p [j + 1] = temp; }}} - IWProgrammer
    • If you had first stated how to get p , what does it really represent ... - Harry

    If you mean that the initial array of students should not be sorted, but only an array of pointers to the initial array is sorted, then the program, for example, might look like this

     #include <iostream> #include <iterator> #include <algorithm> struct Student { std::string name; std::string surname; int test1; }; struct SortStudents { static bool byName(const Student *first, const Student *second) { return first->name < second->name; } static bool bySurname(const Student *first, const Student *second) { return first->surname < second->surname; } static bool byTest(const Student *first, const Student *second) { return first->test1 < second->test1; } }; std::ostream & operator <<(std::ostream &os, Student &st) { return os << st.name << ' ' << st.surname << ' ' << st.test1; } int main() { Student students[] = { { "A", "YY", 10 }, { "C", "ZZ", 6 }, { "B", "XX", 12 } }; const size_t N = sizeof(students) / sizeof(*students); Student *pStudents[N]; for (size_t i = 0; i < N; i++) pStudents[i] = &students[i]; std::cout << "Sorted by name" << std::endl; std::sort(std::begin(pStudents), std::end(pStudents), SortStudents::byName); for (auto p : pStudents) { std::cout << *p << std::endl; } std::cout << "\nSorted by surname" << std::endl; std::sort(std::begin(pStudents), std::end(pStudents), SortStudents::bySurname); for (auto p : pStudents) { std::cout << *p << std::endl; } std::cout << "\nSorted by tests" << std::endl; std::sort(std::begin(pStudents), std::end(pStudents), SortStudents::byTest); for (auto p : pStudents) { std::cout << *p << std::endl; } return 0; } 

    Output of the program to the console

     Sorted by name A YY 10 B XX 12 C ZZ 6 Sorted by surname B XX 12 A YY 10 C ZZ 6 Sorted by tests C ZZ 6 A YY 10 B XX 12 

      Everything, I did it. Below is the program code, col is the number of students

       Students **p; p = new Students*[col]; for (int i = 0; i < col; i++) { p[i] = &student[i]; } for (int i = 0; i < col - 1; i++) { for (int j = 0; j < col - i - 1; j++) { if (p[j]->test1 + p[j]->test2 + p[j]->test3 < p[j+1]->test1 + p[j+1]->test2 + p[j+1]->test3) { Students *temp = p[j]; p[j] = p[j + 1]; p[j + 1] = temp; } } } 
      • If you use the label с++ - feel free to use with ++)) - Majestio