这是我的代码,但它说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)
你需要#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.
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |