C/C++

Как перебрать объект класса записанный в list?

A7
Alarm 74
6
// Пример, например...

#include <iostream>
#include <string>
#include <list>
using namespace std;
struct Record {
string name;
int age;
friend ostream& operator<<(ostream& out, const Record& record) {
return out << record.name << ", " << record.age << '\n';
}
friend bool operator<(const Record& a, const Record& b) {
return a.name < b.name;
}
friend bool operator>(const Record& a, const Record& b) {
return a.name > b.name;
}
friend bool operator==(const Record& a, const Record& b) {
return a.name == b.name;
}
friend bool operator!=(const Record& a, const Record& b) {
return a.name != b.name;
}
friend bool operator<=(const Record& a, const Record& b) {
return a.name <= b.name;
}
friend bool operator>=(const Record& a, const Record& b) {
return a.name >= b.name;
}
};
class Table {
public:
Table(string title) : title_(title) {}
void add(const Record& record) {
records_.push_back(record);
}
void add(Record&& record) {
records_.emplace_back(record);
}
void sort() {
records_.sort();
}
void show()const {
for (const auto& record : records_) cout << record;
}
private:
string title_;
list<Record> records_;
};
int main() {
system("chcp 1251 > nul");
Table students("Студенты");
students.add({"Иванова Ирина Алексеевна", 19});
students.add({ "Герасимов Олег Викторович", 18 });
students.add({ "Третьякова Анна Васильевна", 20 });
students.add({ "Рабинович Абрам Исаакович", 19 });
students.add({ "Валиев Равиль Базарбаевич", 21 });
students.sort();
students.show();
system("pause > nul");
}
Marin Buga
Marin Buga
57 895
Лучший ответ