代码:
#include using namespace std; #include #include #include #include // 员工类 class Employee { public: Employee(string name,int salary) { this->m_Name = name; this->m_Salary = salary; } string m_Name; // 姓名 int m_Salary; // 薪水 }; // 部门类 class Department { public: Department(string name, int no) { this->m_name = name; this->m_No = no; } string m_name; // 部门名字 int m_No; // 部门编号 }; // 仿函数实现 map 容器自定义排序 class MyCompare { public: bool operator()(int v1,int v2)const{ return v1 < v2; } }; // 按照部门打印员工 void printEmployee(const multimap&m,const vector&v,int depNo){ cout << "下面打印:" << v.at(depNo).m_name << "的 成员信息!" << endl; for (multimap::const_iterator it = m.begin(); it != m.end(); it++) { if (depNo == it->first) { cout << "部门:" << v.at(it->first).m_name << " "; cout << "姓名:" << it->second.m_Name << " "; cout << "薪水:" << it->second.m_Salary << endl; } } cout << endl; } void test() { srand(time(0)); // 部门信息初始化 Department d1("策划部", 1); Department d2("美术部", 2); Department d3("研发部", 3); vectorv; v.push_back(d1); v.push_back(d2); v.push_back(d3); string names[] = {"A","B","C","D","E","F","G","H","I","J"}; multimapm; int dep = 1; // 随机部门 int salary = 0.0; // 随机薪水 for (int i = 0; i < 10; i++) { // 随机数初始化 dep = rand() % 3; salary = rand() % 5000 + 1500; // 创建员工 Employee temp(names[i], salary); pair p(dep, temp); m.insert(p); } // 打印策划部成员信息 printEmployee(m, v, 0); // 打印美术部成员信息 printEmployee(m, v, 1); // 打印研发部成员信息 printEmployee(m, v, 2); } int main() { test(); system("pause"); return 0; }
(F_F) !
上一篇 ubuntu20.04 安装Dukto
下一篇 Debian10配置静态ip
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号