如何用另一个字符串替换字符串中的字符?在c ++中

man*_*eja 2 c++

这是我的代码,但它说replace is not declared in this scope这不是正确的语法?

#include<iostream>                                                                          
#include<string>                                                                            


using namespace std;                                                                        

int  main ()                                                                                
{                                                                                           
  string string_to_edit;                                                                    
  cout<<"Enter a string to replace all the vowels:"<<endl;                                  
  cin>>string_to_edit;                                                                      
  string output_string=replace(string_to_edit.begin(),string_to_edit.end(),"a","x");               
  cout<<output_string<<endl;                                                                
  return 0;                                                                                 
} 
Run Code Online (Sandbox Code Playgroud)

jua*_*nza 5

你需要#include <algorithm>std::replace,但你还需要使用单个字符.请注意单引号:

replace(string_to_edit.begin(),string_to_edit.end(),'a','x');  
Run Code Online (Sandbox Code Playgroud)

另请注意,replace将替换元素到位.std::replace回报void.