c++ - 如何在具有继承 C++ 的对象向量上使用基本函数
问题描述
我有class Employee
, 和Intern
派生自Employee
. 我想将员工信息存储在其中并在like等vector<>
上使用基本功能。据我所知,我必须使用指针。问题是我不知道如何使用这些功能这是我正在尝试做的一个例子:vector
sort()
find_if
vector< * >
vector<unique_ptr<Employee>> Firm;
hireIntern(Firm);
//////////////////////////////
void hireIntern(vector<unique_ptr<Employee>>& sourceIntern) {
string fillName;
string fillSurname;
cout << endl;
cout << "Enter Intern Name: ";
cin >> fillName;
cout << "Enter Intern Surname: ";
cin >> fillSurname;
Intern newIntern(fillName, fillSurname);
newIntern.setID();
newIntern.Hire();
newIntern.setSalary(1500);
while (true) { /* 1 */
auto it = find_if(sourceIntern.begin(), sourceIntern.end(),
[&newIntern](const Employee &obj) { return obj.getID() == newIntern.getID(); });
if (it != sourceIntern.end()) {
newIntern.setID();
} else {
break;
}
}
cout << newIntern.getName() << " " << newIntern.getSurname() << " (" << newIntern.getID()
<< ") has been hired" << endl;
sourceIntern.emplace_back(new Intern());
sortEmployeeIDs(sourceIntern);
}
/* 2 */
void sortEmployeeIDs(vector<unique_ptr<Employee>>& sourceEmployee) {
sort(sourceEmployee.begin(), sourceEmployee.end(), [&sourceEmployee](const Employee &left, const Employee &right) {
return left.getID() < right.getID();
});
}
编辑:更新的代码,现在的问题是 Object 似乎没有像我尝试的那样保存在向量中cout << i->getID();
void hireIntern(vector<unique_ptr<Employee>>& sourceIntern) {
string fillName;
string fillSurname;
cout << endl;
cout << "Enter Intern Name: ";
cin >> fillName;
cout << "Enter Intern Surname: ";
cin >> fillSurname;
Intern newIntern(fillName, fillSurname);
newIntern.setID();
newIntern.Hire();
newIntern.setSalary(1500);
while (true) {
auto it = find_if(sourceIntern.begin(), sourceIntern.end(),
[&newIntern](const unique_ptr<Employee> &obj) { return obj->getID() == newIntern.getID(); });
if (it != sourceIntern.end()) {
newIntern.setID();
} else {
break;
}
}
cout << newIntern.getName() << " " << newIntern.getSurname() << " (" << newIntern.getID()
<< ") has been hired" << endl;
sourceIntern.emplace_back(new Intern());
sortEmployeeIDs(sourceIntern);
for(const auto &i : sourceIntern) {
cout << i->getID();
}
}
解决方案
两者std::find_if
和std::sort
都会将向量的元素传递给您的比较函子,就像 bytest_func(*iter)
或一样compare_func(*iter1, *iter2)
。您已经编写了 lambdas 来接受const Employee&
参数,但向量元素实际上是std::unique_ptr<Employee>
对象。
如果您编写 lambda 以采用正确的类型,它应该可以工作。(另外,请注意您不需要sourceEmployee
在比较 lambda 中捕获,因为您不使用它。)
[&newIntern](std::unique_ptr<Employee>& ptr)
{ return ptr->getID() == newIntern.getID(); }
[](std::unique_ptr<Employee>& left, std::unique_ptr<Employee>& right)
{ return left->getID() < right->ID(); }
我知道您标记了问题 [c++11],但如果您可以使用 C++14 或更高版本,您可以编写auto&
而不是std::unique_ptr<Employee>&
作为 lambda 参数。
推荐阅读
- python - 找不到满足要求 tensorflow 的版本 - 在 docker 内
- mysql - 当 WHERE 子句中的一个字段为空时,MySQL 数据库是否使用复合索引?
- html - 如何显示完整图像而不是方形图像的部分图像
- sql - SQL根据列值条件返回行
- typescript - 如何为打字稿枚举类型 {[key]: value } 生成文档?
- ffmpeg - FFMPEG 将 m3u8 重新流式传输到 Youtube 在第一段之后不断返回“无法更新具有正确持续时间和文件大小的标头”
- r - R中按组月的平均温度
- swiftui - 删除所选项目时 SwiftUI 列表崩溃
- python - 如何标记按熊猫箱线图分组的?
- wordpress - 自动生成的页面文件