1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
int id;
string name;
int age;
Student(int i, string n, int a) : id(i), name(n), age(a) {}
};
ostream& operator<<(ostream& os, const Student& s) {
return os << s.id << ", " << s.name << ", " << s.age << '\n';
}
int main() {
vector<Student> s;
s.push_back(Student(7, "bob", 20));
s.push_back(Student(3, "joe", 21));
s.push_back(Student(9, "sue", 22));
for (const auto &x: s) cout << x; cout << '\n';
sort(s.begin(), s.end(),
[](Student& a, Student& b){return a.id < b.id;});
for (const auto &x: s) cout << x; cout << '\n';
cout << "Min id: " << s[0].id << '\n';
cout << "Max id: " << s.back().id << '\n';
// or s[s.size()-1].id
}
| |