4 (Alice, 85) (Bob, 92) (Charlie, 78) (David, 85) (conceptual input; internally stored as pair<int,int> or pair<string,int>)
Charlie 78 Alice 85 David 85 Bob 92
bool compareBySecond(pair<string, int> a, pair<string, int> b) {
return a.second > b.second; // Descending order
}
void stl_q11_custom_sort() {
vector<pair<string, int>> students;
students.push_back(make_pair("Raj", 85));
students.push_back(make_pair("Priya", 92));
students.push_back(make_pair("Amit", 78));
sort(students.begin(), students.end(), compareBySecond);
cout << "Students sorted by marks:" << endl;
for(auto s : students) {
cout << s.first << " : " << s.second << endl;
}
}
bool compareBySecond(pair<string, int> a, pair<string, int> b) {
return a.second > b.second; // Descending order
}
void stl_q11_custom_sort() {
vector<pair<string, int>> students;
students.push_back(make_pair("Raj", 85));
students.push_back(make_pair("Priya", 92));
students.push_back(make_pair("Amit", 78));
sort(students.begin(), students.end(), compareBySecond);
cout << "Students sorted by marks:" << endl;
for(auto s : students) {
cout << s.first << " : " << s.second << endl;
}
}