在C ++中的单独头文件中完成函数的主体?

时间:2018-11-03 17:25:14

标签: c++ object pointers vector stl

我有一个家庭作业任务,应完成位于单独文件Find.h中的函数的主体,该函数的完成应使以下编写的代码可以成功编译:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "Find.h"
using namespace std;
class Company {
    std::string name;
    int id;
public:
    std::string getName() const {
        return this->name;
    }

    int getId() const {
        return this->id;
    }

    friend std::istream& operator>>(std::istream& stream, Company& company);
};

std::istream& operator>>(std::istream& stream, Company& company) {
    return stream >> company.name >> company.id;
}

std::ostream& operator<<(std::ostream& stream, const Company& company) {
    return stream << company.getName() << " " << company.getId();
}

int main() {
    using namespace std;
    vector<Company*> companies;
    string line;
    while (getline(cin, line) && line != "end") {
        istringstream lineIn(line);

        Company* c = new Company();
        lineIn >> *c;
        companies.push_back(c);
    }

    string searchIdLine;
    getline(cin, searchIdLine);
    int searchId = stoi(searchIdLine);

    Company* companyWithSearchedId = find(companies, searchId);

    if (companyWithSearchedId != nullptr) {
        cout << *companyWithSearchedId << endl;
    }
    else {
        cout << "[not found]" << endl;
    }

    for (auto companyPtr : companies) {
        delete companyPtr;
    }

    return 0;
}

这是我完成Find.h文件的不完整尝试(程序应输出ID和与给定ID相匹配的公司名称):

#ifndef FIND_H
#define FIND_H
#include "Company.h"
#include <vector>
using namespace std;
Company* find(vector<Company*> vc, int id) {

    for (int i = 0; i < vc.size(); i++) {
        if (vc[i]->getId() == id) {
            //I do not know what to write here as to return a pointer 
            //to the required element so as to fulfil the requirement?
        }
    }
    return nullptr;
}
#endif // !FIND_H

2 个答案:

答案 0 :(得分:0)

一种选择是定义 functor 或函数对象,并使用Uploads算法:

std::find

编辑1:无需struct Find_By_ID { int id_to_find; bool operator==(const Company& a) { return a->getId() == id_to_find; } } //... std::vector<Company> database; // Note, not a vector of pointers //... Find_By_ID functor; functor.id_to_find = searchId; std::vector<Company>::iterator iter = std::find(database.begin(), database.end(), functor);
构建数据库时,您无需使用new

new

Company c; std::vector<Company> database; while (std::cin >> c) { database.push_back(c); } 方法将进行复制并将其附加到向量中。向量将根据需要为该项分配内存。

编辑2:蛮力
您可以使用自定义的蛮力方法而不是函子:

std::vector::push_back()

如果要修改找到的const std::vector<Company>::const_iterator iter_begin(database.begin()); const std::vector<Company>::const_iterator iter_end(database.end()); std::vector<Company>::const_iterator iter; for (iter = iter_begin; iter != iter_end; ++iter) { if (iter->getId() == searchId) { break; } } if (iter != iter_end) { std::cout << "Customer found by ID.\n"; } ,请将迭代器类型更改为:
Customer

答案 1 :(得分:0)

对于.h文件中用于循环的特定问题,请尝试:

return vc[i]; //vc is a vector of Company pointers, this returns the pointer at vc index i

对于输出部分,请考虑:

cout << companyWithSearchedId->getId() << " " << companyWithSearchId->getName() << endl;

总体而言,这里还有更多问题,请花一些时间并逐步解决问题。