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

// Пример, например...
#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");
}
#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");
}
Похожие вопросы
- Динамические массивы объектов класса. Нужна помощь.
- Доработка класса. Создание массива объектов
- Лабораторная работа. Классы. Передача объекта в качестве аргумента.
- Программирование на С++(Классы-объекты)
- Работа с классами и объектами
- Что сложного в этом вашем Си плюс плюсе? Классы, объекты, конструкторы, наследование, полиморфизм?
- Классы, функции, объекты
- Найти сумму цифр чисел, записанных в файл
- Где располагается нессылочная переменная в ссылочном объекте с++?
- Можно ли через функцию memset записать число в элемент массива больше чем 255? (Си)