Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Students
- {
- string name;
- string surname;
- int test1;
- };
- Students student[100];
- #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;
- }
- До сортировки:
- Иванов Иван : 3
- Петров Петр : 2
- Степанов Степан : 4
- После сортировки:
- Степанов Степан : 4
- Иванов Иван : 3
- Петров Петр : 2
- #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;
- }
- 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;
- }
- }
- }
- #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;
- }
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement