c ++如何将数组中的字符串替换为另一个字符串

The*_*n99 0 c++ arrays string

我正在尝试一个使用数组的短代码,当我调用我的函数WordReplace时,我基本上想要替换仇恨这个词,但是我继续打印相同的东西:

我不爱c ++我不喜欢c ++

我尝试了不同的东西,但我不确定是什么问题

#include <iostream>
#include <string>
using namespace std;

void WordReplace(string*x, int start, int end, string g, string w)
{
   for (int z = start; z <= end; z++)
   {
      if (x[z] == g)
         x[z] == w;

      cout << x[z]<<" ";
   }
}

int main()
{
   string x[4] = {"I", "don't", "hate", "c++"};

   for (int i = 0; i < 4; i++)
   {
      cout << x[i] << " ";
   }
   cout << endl;

   WordReplace(x, 0, 3, "hate", "love");

   cout << endl;

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

Sla*_*ica 5

只需使用std :: replace:

std::string x[] = {"I", "don't", "hate", "c++"};
std::replace( std::begin( x ), std::end( x ), "hate", "love" );
Run Code Online (Sandbox Code Playgroud)

实例