Untitled

 avatar
unknown
c_cpp
a year ago
1.4 kB
13
Indexable
#include <bits/stdc++.h>
using namespace std;

struct Student {
    int age;
    string name;
    int classs;
};

typedef void (*AddStudentCallback)(Student&);

void callbackfunction(Student& student) {
    cout << "Name: " << student.name << endl;
    cout << "Age: " << student.age << endl;
    cout << "Class: " << student.classs << endl;
    cout << "--------------------------" << endl;
}

void addstudent(vector<Student>& students, Student& stu, AddStudentCallback callback) {
    students.push_back(stu);
    if (callback != nullptr) {
        callback(stu);
    }
}

int main() {
    vector<Student> students;
    int n;
    cout<<"Enter number of students "<<endl;
    cin>>n;
    for (int i = 0; i < n; i++) {
        int k,j;
        string name;
        cout<<"Give student age, name, class"<<endl;
        cin>>k>>name>>j;
        Student stu = { k , name, j};
        addstudent(students, stu, callbackfunction);
    }
    sort(students.begin(), students.end(), [](Student& a,Student& b)
    {
        return (a.age<b.age);
    });
    cout <<"After sorting by age"<<endl;
    for(int i =0 ; i<n;i++)
    {
        cout << "Name: " << students[i].name << endl;
        cout << "Age: " << students[i].age << endl;
        cout << "Class: " << students[i].classs << endl;
        cout << "--------------------------" << endl;
    }

    return 0;
}
Editor is loading...
Leave a Comment