首页 > 解决方案 > 如何在具有继承 C++ 的对象向量上使用基本函数

问题描述

我有class Employee, 和Intern派生自Employee. 我想将员工信息存储在其中并在like等vector<>上使用基本功能。据我所知,我必须使用指针。问题是我不知道如何使用这些功能这是我正在尝试做的一个例子:vectorsort()find_ifvector< * >

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();
    }
}

标签: c++functionc++11inheritancevector

解决方案


两者std::find_ifstd::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 参数。


推荐阅读