C ++ <algorithm>替换所有不起作用的事件

时间:2017-06-29 13:44:25

标签: c++ string algorithm replace

我正在尝试用C ++(11)制作一个简单的猜词游戏,因为它需要向玩家展示"hidden word"我已经制作了两个字符串;第一个可见"test",另一个只有下划线"____"。我用string::iterator创建了一个循环来比较字符串中的每个字符与用户输入的给定(char)。

这里的问题是,我第一次输入&#39; t&#39;&#39; t__t&#39;它不会替换字符串中的字符串。这应该导致&#34; t___&#34;而不是&#34; replace&#34;

我正在使用<algorithm>标题中的replace(str.begin(), str.end(), str.at(char_idx), newLetter); 函数 看起来像这样:

checkLetter()


并且此函数位于foor循环内部,该循环遍历字符串;在另一个名为void checkLetter(char &newLetter, string &randstr, int &strlen, string &hiddenWord){ string::iterator it; char char_idx; for(auto it = randstr.begin(); it != randstr.end();++it){ if(*it == newLetter){ char_idx=randstr.find(*it); replace(hiddenWord.begin(), hiddenWord.end(), hiddenWord.at(char_idx), newLetter); } } cout << hiddenWord << endl; }

的功能中
The Word is 4 letters long.

t
t___
e
te__
s
tes_
t
tes_

现在这就是输出的样子:

string RandomWord = "test";
replace(RandomWord.begin(),RandomWord.end(),'t','S');
cout << RandomWord << endl;

但是当我运行更简单的版本时,例如

'SesS'

,提供#include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> using namespace std; // Function Declaration string getRandWord(); void checkLetter(char&, string&, int&, string&); int main() { // string RandomWord = getRandWord(); string RandomWord = "test"; string hiddenWord = ""; unsigned long int _length_ = RandomWord.length(); int chances = int(_length_)+1; char newLetter; hiddenWord.append((_length_),'_'); cout << "The Word is "<< _length_ <<" letters long." << endl; while(chances > 0){ cin >> newLetter; checkLetter(newLetter, RandomWord, chances, hiddenWord); chances--; } return 0; } // Functions void checkLetter(char &newLetter, string &randstr, int &strlen, string &hiddenWord){ string::iterator it; char char_idx; for(auto it = randstr.begin(); it != randstr.end();++it){ if(*it == newLetter){ char_idx=randstr.find(*it); replace(hiddenWord.begin(), hiddenWord.end(), hiddenWord.at(char_idx), newLetter); } } cout << hiddenWord << endl; } string getRandWord(){ string filePath = "/Users/nedimkanat/XCODE/testcpp/testcpp/"; enum sizes { ARRAY_SIZE = 5 }; // set seed srand((unsigned)time(0)); // get random int between 0 and 5 int randint = rand() % ARRAY_SIZE; // str to store each line from file string str; // array to store 5 (random) words vector<string> arr; // initialize file object & open file ifstream file(filePath+"words.txt"); int counter = 0; // loop trough file if (file.is_open()){ while (getline(file,str) && counter < ARRAY_SIZE){ arr.push_back(str); counter++; } file.close(); } else { cout << "File is not open" << endl; } // send away random word if(arr.empty()){ cout << "CANCER" << endl; } return arr.at(randint); }

所有代码:

observable1.delay(5000).subscribe(() => {
    alert('ok');
});

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你基本上想要transform一个字符串到另一个字符串?

使用std::transform的双输入迭代器重载应该是您需要的功能。第一个输入容器可以是带有要替换的下划线的字符串,而第二个输入容器是包含实际单词的字符串。然后使用输入字母作为状态或捕获提供函子或lambda,并使函数返回第一个容器中的当前字母(掩码词),除非该字母与当前字母匹配第二个容器(要猜的词)。输出迭代器是掩码字(与第一个输入迭代器相同)。

也许类似这个示例程序的内容显示:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string word = "foobar";  // The word to guess
    std::string masked = "______";  // The masked word, that will be unmasked letter by letter

    // "Guess" some letters...        
    for (auto const letter : { 'f', 'a', 'x', 'o', 'y', 'r', 'b' })
    {
        std::cout << "Before '" << letter << "': \"" << masked << "\"\n";

        // Convert the masked word into a less masked word
        // But only if the letter is found in the word to guess
        std::transform(std::begin(masked), std::end(masked), std::begin(word),
                      std::begin(masked),
                      [letter](char const& masked_letter, char const& word_letter)
                       {
                           if (letter == word_letter)
                               return word_letter;
                           else
                               return masked_letter;
                       });

        std::cout << "After  '" << letter << "': \"" << masked << "\"\n";
    }
}

Example of it running here

答案 1 :(得分:1)

如果基于您的代码,您不应该使用std :: replace,因为第三个参数(意思是替换字母)可以在文本中出现两次。所以你可以替换下面的字母。

void checkLetter(char &newLetter, string &randstr, int &strlen, string &hiddenWord){
    string::iterator it;
    for(auto it = randstr.begin(); it != randstr.end();++it){
        if(*it == newLetter){
            hiddenWord.at(it - randstr.begin()) = newLetter;
        }
    }
    cout << hiddenWord << endl;
}