#include 'collage.h'

using std::cout; using std::endl;

// 构造函数,初始化学院名称 Collage::Collage(const string &name) : m_name(name) {

}

// 添加学生到学院中 void Collage::addStu(const Student &stu) { m_slst.push_back(stu); }

// 自定义谓词类,用于比较学生学号 class Comsid { public: Comsid(const string &sid) : m_sid(sid) {} bool operator() (const Student &stu) { if(m_sid == stu.sid()) { return true; } return false; } private: string m_sid; };

// 删除学院中的学生 void Collage::delStu(const Student &stu) { // 使用remove_if算法和自定义的谓词进行删除 Comsid cmp(stu.sid()); m_slst.remove_if(cmp); }

// 根据学号查找学生 const Student *Collage::student(string sid) { // 遍历学生列表,找到学号匹配的学生,返回指针 for(const auto &stu : m_slst) { if(sid == stu.sid()) { return &stu; } } return nullptr; }

// 显示学院中的学生 void Collage::showStudents() const { // 遍历学生列表,打印每个学生的信息 list::const_iterator it = m_slst.begin(); for(it; it != m_slst.end(); it++) { cout << *it << endl; } }

// 返回学院中的学生列表 list Collage::students() const { return m_slst; }

C++ 学院管理类:Collage 类实现详解

原文地址: https://www.cveoy.top/t/topic/qyQi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录