【发布时间】:2022-01-05 09:38:46
【问题描述】:
class Hangman
{
private:
vector<string> dictionary; //stores all the words
vector<string> secretWord; //stores the secret word
vector<string> misses; //keeps record of wrong guesses
vector<string> displayVector; //Stores "_"
string originalWord; //stores a copy of secret word to display at the
end of game.
bool gameOver = false; //Flag to check if the player lost or
still in the game.
int totalAttempts;
public:
void selectRandWord();
};
//这是我遇到问题的函数。
void Hangman::selectRandWord()
{
secretWord.clear();
//word 是存储随机单词的基本字符串。让我们说“Hello World”。
string word;
srand(time(NULL));
int random = (rand() % dictionary.size()) + 1;
//我从一个向量到另一个词存储一个随机词。
word = dictionary[random];
transform(word.begin(), word.end(), word.begin(), ::tolower);
originalWord = word;
for (int index = 0; index < word.length(); index++)
{
//这行有错误:[Error] invalid user-defined conversion from 'char' to 'std::vectorstd::basic_string
//我要做的是从单词中取出每个字符(例如:“H”)并将其推回向量字符串secretWord中。
secretWord.push_back(word[index]);
}
}
【问题讨论】:
-
为什么
secretWord首先是vector<string>?不应该是string吗? -
我没有在这里发布我的完整程序,但基本上,我希望能够使用向量库函数,如 pop、pushback、erase 等。