我是否为邪恶刽子手的构造函数错过了什么?

Am I missing anything for my constructor for Evil Hangman?

本文关键字:构造函数 错过了 什么 刽子手 是否 邪恶      更新时间:2023-10-16

这是头文件。

#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
class FamilySet
{
public:
FamilySet();
// Default
FamilySet(string file);
// Initializes from a word file
FamilySet(string file, int len);
// Initializes from a word file where the word
// must be of length len.
~FamilySet();
private:
vector<string> masterList;
// This stores all words currently "valid"
unordered_map<string, vector<string>> dictionaries;
// Stores a dictionary for each family. Each word from
// the masterList is contained within one of these
// these vector dictionaries.
int iterCount;    // Used for iterator
};

这是我对构造函数的内容。此构造函数将是唯一相关的构造函数。我有一个想法,我需要初始化向量和无序映射,但不太确定如何。

FamilySet::FamilySet(string file, int len) {
iterCount = 0;
ifstream myFile(file);
string word;
while(myFile >> word)
{
masterList.push_back(word);
}
myFile.close(); 
}

到目前为止,我所看到的在开始使用 while 循环之前您要做的是检查文件是否已正确打开。

您可能还希望将 iterCount 变量放在 while 循环中。

此外,如果您决定使用 len,那么您可以将其作为边界条件合并到您的 while 循环中,以便能够仅读取特定数量的单词。

FamilySet::FamilySet(string file, int len) {
iterCount = 0;
ifstream myFile(file);
if(myFile.fail())
{
cout << "File failed to openn";
}
string word;
// this way you will only read a len amount of words if this is what you want
while(myFile >> word && len--)
{
++iterCount;
masterList.push_back(word);
}
myFile.close(); }