【问题标题】:Completing the body of a function in a separate header file in C++?在 C++ 的单独头文件中完成函数体?
【发布时间】:2019-04-07 14:16:54
【问题描述】:

我有一个家庭作业任务,我应该完成位于单独文件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

【问题讨论】:

  • 恕我直言,您应该将 Company 的类定义放入单独的头文件中,例如company.hpp,以及方法实现到相关的源文件中,例如公司.cpp。接下来,告诉您的编译器或 IDE,您有 main.cppcompany.cpp 文件(如何执行此操作取决于您的 IDE 或构建系统)。

标签: c++ object pointers vector stl


【解决方案1】:

对于.h文件中的具体问题for循环试试:

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

对于输出部分,请考虑:

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

这里总体上还有更多问题,花点时间解决它。

【讨论】:

    【解决方案2】:

    另一种方法是定义一个仿函数或函数对象并使用std::find算法:

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

    编辑 1:不需要new
    构建数据库时不需要使用new

    Company c;
    std::vector<Company> database;
    while (std::cin >> c)
    {
      database.push_back(c);
    }
    

    std::vector::push_back() 方法将创建一个副本并将其附加到向量中。向量将根据需要为项目分配内存。

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

    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,请将迭代器类型更改为:
    std::vector&lt;Customer&gt;::iterator
    视情况而定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 2020-06-01
      相关资源
      最近更新 更多