尝试在字符串中索引字符并创建新字符串
Trying index characters in strings and create new strings
我目前正在制作一个快速的刽子手游戏,我正在努力从单词中获取正确猜测的字母并将它们插入到我在用户玩游戏时向用户展示的字符串中。这是我到目前为止的代码:
std::string word_to_guess = "ataamataesaa";
std::string word_to_fill(word_to_guess.length(), '-');
char user_guess = 'a';
for (auto &character : word_to_guess) {
if (character == user_guess) {
std::size_t index = word_to_guess.find(&character);
word_to_fill[index] = user_guess;
}
}
std::cout << word_to_fill;
这几乎有效,但是它忽略了字符串的最后两个 As 来猜测我无法理解的内容。
"Find" 将仅返回第一次出现。
不要循环访问字符,而是同时迭代word_to_guess和word_to_fill的索引。
for (int i = 0 ; i < word_to_fill.length ; ++i) {
if (word_to_guess[i] == user_guess) {
word_to_fill[i] = user_guess;
}
}
相关文章:
- 从std::字符串创建OssBitString
- 如何从字符串创建lua表?
- 如何使用循环从另一个字符串创建一个没有空格的新字符串
- 如何从字符串创建一个web::uri以放入client.connect()
- 如何从字符串创建固定的QDateTime
- 用C 的字符串创建一系列字符串
- 正在尝试为c字符串创建一个Text Line类
- 如何使用字符串创建条件
- 优化字符串创建
- C++:使用子字符串创建新字符串
- 从字符串创建对象 (C++)
- 通过本机字符串创建 BSTR(使用_bstr_t包装器)时,如何设置长度
- 基于另一个字符串创建一个由空格 (_) 和空格组成的字符串
- 有没有更好的方法从字符串创建目录?CPP,创建目录,字符串流,字符串
- 在 c++ 中从字符串创建类以避免大型 switch 语句
- 从字符串C++创建控制台标题
- C++:从字符串创建参数化指针
- 在C++中为字符串创建自定义函数
- 如何为字符串创建要起作用的unordered_map
- 如何从二进制字符串创建一个bitset