Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在继承C++;_C++_Function_C++11_Inheritance_Vector - Fatal编程技术网

C++ 如何在继承C++;

C++ 如何在继承C++;,c++,function,c++11,inheritance,vector,C++,Function,C++11,Inheritance,Vector,我有class Employee,还有classIntern,它们来自Employee。我想在vector中存储员工信息,并使用vector上的基本功能,如sort(),查找if等。据我所知,我必须使用指针。问题是我不知道如何在vector上使用这些函数,下面是我尝试执行的一个示例: vector<unique_ptr<Employee>> Firm; hireIntern(Firm); ////////////////////////////// void hireI

我有
class Employee
,还有class
Intern
,它们来自
Employee
。我想在
vector
中存储员工信息,并使用
vector
上的基本功能,如
sort()
查找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();
        });
}
向量公司;
雇佣国际(商号);
//////////////////////////////
void hireIntern(向量和源实习生){
字符串填充名;
姓氏;
不能填写姓名;
姓氏;
实习生新实习生(姓名、姓氏);
newIntern.setID();
newIntern.Hire();
新实习生setSalary(1500);
while(true){/*1*/
auto it=find_if(sourceIntern.begin(),sourceIntern.end(),
[&newIntern](constemployee&obj){return obj.getID()==newIntern.getID();});
if(it!=sourceIntern.end()){
newIntern.setID();
}否则{
打破
}
}

cout这两个
std::find_if
std::sort
将向量的元素传递给比较函子,就像通过
test_func(*iter)
compare_func(*iter1,*iter2)一样
。您已经编写了lambda来获取
const Employee&
参数,但是向量元素实际上是
std::unique\u ptr
对象

如果您编写lambda以采用正确的类型,它应该可以工作。(另外,请注意,您不需要在比较lambda中捕获
sourceEmployee
,因为您不使用它。)

[&newIntern](std::unique\u ptr&ptr)
{return ptr->getID()==newIntern.getID();}
[](标准::唯一左、右)
{return left->getID()ID();}

我知道你给问题加了标签[c++11],但如果您可以使用C++14或更高版本,您可以编写
auto&
而不是
std::unique\u ptr&
作为lambda参数。

在第2点尝试对指针进行排序,但为非指针提供比较。可能在第1点出现类似问题。感谢您解决了我的问题。现在函数运行良好,我学习了如何实现它们。当谈到你提到的
while
循环时,让我解释一下我在这里做什么。while循环是为了避免为员工设置重复的ID。我正在执行
查找\u if
以查找重复的ID。如果没有问题,我退出循环,如果有重复的,我会执行
设置ID()
(mt19937随机数,从10k-9,9k开始)然后再循环一次,看看是不是另一个重复。所以现在我不明白你给我的解决方案。@Taknie哦,我明白了。所以你每次都想扫描整个向量。那就不用管最后一部分了。再次感谢你的帮助。你的答案是解决方案。现在我要在我的数据库中实现其余的函数,不是吗这样。如果出现错误,我会再次回复并询问,如果你不介意的话:)@Taknie如果你在编译或运行程序时遇到另一个问题,请打开一个新的问题来描述这个问题。我更新了帖子。你能看一下吗?我想我必须使用
emplace\u back
而不是
push\u back
,但是将信息复制到vector不起作用。
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();
    }
}
[&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(); }