c++ - Substring Replace Vector -
this minimal code sample
i'm trying create array of substrings find can replace them single word. in case i'm changing common greetings simple 'hi'.
the problem when run code i'm getting error.
error: no matching function call 'std::vector >::push_back(const char [4], const char [4], const char [3])'
if me understand why error occurring , suggest solution perfect.
#include <iostream> #include <string> #include <algorithm> #include <cctype> #include <ctime> #include <vector> vector<string> hiword; hiword.push_back("hey", "sup", "yo"); (const auto& word : hiword){ while (true) { index = r.find(word); if (index == string::npos) break; r.replace(index, word.size(), "hi"); } }
you might want start creating vector of strings want search , replace:
vector<string> searchwords = {"hey", "hello", "sup"};
then use loop run code wrote, e.g.
for (const auto& word : searchwords) { while (true) { index = r.find(word); if (index == string::npos) break; r.replace(index, word.size(), "hi"); } }
Comments
Post a Comment