我只是对我的文件应该是什么样子感到困惑。我不确定语法以及如何读取数组。
最佳答案
.cpp 文件。它应该都在一个头文件中。阅读以下
了解更多信息。
Why can templates only be implemented in the header file?
Map
类中没有构造函数声明它以
std::string
作为参数。提供一份!template <typename Domain, typename Range>
class Map
{
public:
Map(const std::string& filename); // declare one constructor which takes std::string
// ... other members
};
参数。
template <typename Domain, typename Range> // ---> this
void Map<Domain, Range>::add(Domain d, Range r)
{
// implementation
}
template <typename Domain, typename Range> // ---> this
bool Map<Domain, Range>::lookup(Domain d, Range& r)
{
// implementation
}
Map
类必不可少,作为分配的内存(使用 new
)应该被释放。因此,相应地执行 The rule ofthree/five/zero 。
话虽如此,如果您可以使用
std::vector
,则可以避免手动内存管理。#include <vector>
template <typename Domain, typename Range>
class Map
{
public:
//...
private:
// other members
std::vector<Domain> dArray;
std::vector<Range> rArray;
};
作为旁注,避免使用
using namespace std;
练习。Why is "using namespace std;" considered bad practice?
关于c++ - map 模板类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57985175/