用C++在单独的头文件中完成函数体
Completing the body of a function in a separate header file in C++?
我有一个家庭作业任务,我应该完成位于单独文件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
一种替代方案是定义函子或函数对象,并使用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<Customer>::iterator
。
对于循环的.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;
作为一个整体,这里还有更多的问题,请慢慢来解决。
相关文章:
- 用C++在单独的头文件中完成函数体
- 我可以在 C++ 中的函数体之外进行操作吗?
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- 无法从 SO 文件调用 SO 文件的函数 - C++生成文件
- binary_search() 函数在我的函数体中不起作用
- 如何显示函数开始、结束行和函数体?
- 主函数体未检测到对重载可变变量模板化函数C++的调用
- 使用函数打开文件,然后让其他函数利用该文件?
- 错误:函数声明符之后的预期函数体
- 将动态方法添加到类中:使用宏函数到头文件中
- 无法从另一个函数访问文件范围变量的内容
- C++编译器如何检测非常量函数体
- 通过函数将文件读取到矢量中
- 使用setW()函数保存文件时,如何获取每个属性的值
- 使用函数读取文件.txt中的矩阵
- UNIX API 调用:使用 read() 函数打开文件并将其打印到屏幕上会增加额外的随机字符
- 使用委托调用构造函数和在另一个构造函数体中调用的区别
- 使用 getline 函数读取文件,但第一列显示为空
- Flex和Bison忽略了函数体
- 为什么将函数体放置在单独的源代码文件而不是标题中的C/C 中