如何将基本字符串中的字符复制到向量字符串中?
How to copy a character from basic string into a vector string?
//定义class
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是一个存储随机单词的基本字符串。让我们说“你好世界”。
string word;
srand(time(NULL));
int random = (rand() % dictionary.size()) + 1;
//我把一个随机词从vector存到word中。
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<char >::value_type&& {aka std::basic_string&&}' [-fpermissive]
//我想做的是从单词中取出每个字符(例如:“H”)并将其推回向量字符串 secretWord。
secretWord.push_back(word[index]);
}
}
您的 secretWord
现在是 vector<string>
类型,因此它可能是许多单词的集合,我不确定这是否是您真正想要的名称。如果确实没问题,并且您想将 word
中的每个字符作为单独的 string
存储在 secretWord
中,那么您需要将 push_back
调用替换为 emplace_back
调用另一个方法实际上一次做两件事:从传递给它的 char
构造一个 string
并将 string
附加到容器的末尾,像这样
secretWord.emplace_back(1, word[index]);
Mere push_back
失败,因为需要为它提供您的 vector
存储类型的对象,因此您也可以通过显式构建 string
来解决您的问题char
:
secretWord.push_back(string(1, word[index]));
我建议您阅读这些内容:emplace back reference and push back reference 如果您对 copy/move 详细信息感兴趣。
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是一个存储随机单词的基本字符串。让我们说“你好世界”。
string word;
srand(time(NULL));
int random = (rand() % dictionary.size()) + 1;
//我把一个随机词从vector存到word中。
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<char >::value_type&& {aka std::basic_string&&}' [-fpermissive]
//我想做的是从单词中取出每个字符(例如:“H”)并将其推回向量字符串 secretWord。
secretWord.push_back(word[index]);
}
}
您的 secretWord
现在是 vector<string>
类型,因此它可能是许多单词的集合,我不确定这是否是您真正想要的名称。如果确实没问题,并且您想将 word
中的每个字符作为单独的 string
存储在 secretWord
中,那么您需要将 push_back
调用替换为 emplace_back
调用另一个方法实际上一次做两件事:从传递给它的 char
构造一个 string
并将 string
附加到容器的末尾,像这样
secretWord.emplace_back(1, word[index]);
Mere push_back
失败,因为需要为它提供您的 vector
存储类型的对象,因此您也可以通过显式构建 string
来解决您的问题char
:
secretWord.push_back(string(1, word[index]));
我建议您阅读这些内容:emplace back reference and push back reference 如果您对 copy/move 详细信息感兴趣。