Dan array of words. It is necessary to replace all the letters QQ in words with the letters BB. (For example, 4 words) After that, get a substring, taking two final letters from each array word longer than 1 character. To form a sentence from those array words that do not contain characters from the resulting substring. Rummaged through all the forums. Nothing helps. I tried to do through the initial stage through for_each, search, replace, find.

#include <iostream> #include <algorithm> #include <vector> #include <iterator> #include <string> using namespace std; void myfunction(string & i) { if (i == "QQ") i = "BB"; } int main() { vector<string> v; v.push_back("QQ"); v.push_back("creamQQ"); v.push_back("bQQbus"); v.push_back("cQQb"); v.push_back("QQcci"); ostream_iterator<string> printit(cout, " "); cout << "Before replacing" << endl; copy(v.begin(), v.end(), printit); for_each(v.begin(), v.end(), myfunction); cout << endl; cout << "After replacing" << endl; copy(v.begin(), v.end(), printit); cout << endl; system("pause"); return 0; } 

And even somehow, and all for nothing = /

  • In myfunc, you compare a string for an exact match, and you need to look for such a substring. - iksuy
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

I think that will suit you

 void myfunction(string & i) { for(auto idx = i.find("QQ"); idx != string::npos; idx = i.find("QQ")) i.replace(idx,2,"BB"); } 

This is the answer to the question put into the title - "replace all QQ with BB".

After that, get a substring, taking two final letters from each word of the array longer than 1 character. To form a sentence from those array words that do not contain characters from the resulting substring.

This is confusing. If we take 2 letters from each word, then any word will automatically contain at least 2 characters from the resulting substring. Those. as a result, we should not get anything. This is hardly what is required, so if you are interested in this part, then specify it.

    Search string:

     for (std::vector<string>::iterator it = myvector.begin(); it != myvector.end(); ++it) { int found=it->find("QQ"); if (found == std::string::npos) break; it->replace(found, 2, "BB"); } 
    • one
      Those. if in the first line there is no this "Ku-ku", then in the others it is not necessary to search? And if the "Ku-ku" a few? - Harry
    • add an extra check, it's not difficult - Alexsandr Ter
    • And after all - you need to either write (*it). or it-> . - Harry
    • Of course, easy. But if a person starts writing instead of searching, if (i == "QQ") i = "BB"; , he is unlikely to cope with this on his own ... - Harry