我是否为邪恶刽子手的构造函数错过了什么?
Am I missing anything for my constructor for Evil Hangman?
这是头文件。
#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(); }
相关文章:
- 我用字符串做了一个C++构造函数,但它不会打印出字符串
- 我是否为邪恶刽子手的构造函数错过了什么?
- 删除了在初始值设定项中调用的构造函数
- C++:将向量传递给函数,然后在main中调用函数.错过了什么
- emplace_back和push_back给出错误'double free or corruption (fasttop)'尽管定义了复制和移动构造函数
- 解释了构造函数成员初始化列表
- std::allocator_traits::construct调用了错误的构造函数
- 删除了所有自动生成的构造函数/运算符的类仍然可以从函数返回吗
- 编译器错过了无效的构造函数调用,并调用不存在的(或私有的)默认构造函数
- C++标准是否对向量赋值函数或构造函数提出了明确的要求
- 复制构造函数..怎么了
- 模板化构造函数失去了右值
- 乍一看,构造函数返回了一个对象
- 我的复制构造函数出了什么问题
- 接受对象作为参数的构造函数发生了什么
- 如果我在默认构造函数中放了一个参数,但给了这个参数一个默认值,它还是一个默认构造函数吗
- 复制构造函数调用了两次
- 复制构造函数调用了 2 次,而不是预期的 3 次
- 类构造函数声明了两次
- 其中一个 QImage 构造函数给了我一个链接器错误,但另一个构造函数没有?