C++ 用c+中的另一个ascii字符替换一个ascii字符的所有实例+;?
可能重复:C++ 用c+中的另一个ascii字符替换一个ascii字符的所有实例+;?,c++,C++,可能重复: 例如,我有一个字符串“Hello World”,我想用“1”s替换所有的“l”。我该怎么做?我是C++新手。我的大部分背景都是Python,在Python中,您可以只使用.replace方法。使用std::replace #include <string> #include <algorithm> #include <iostream> int main() { std::string str = "Hello World";
例如,我有一个字符串“Hello World”,我想用“1”s替换所有的“l”。我该怎么做?我是C++新手。我的大部分背景都是Python,在Python中,您可以只使用.replace方法。使用
std::replace
#include <string>
#include <algorithm>
#include <iostream>
int main() {
std::string str = "Hello World";
std::replace(str.begin(),str.end(),'l','1');
std::cout << str; //He11o Wor1d
}
#包括
#包括
#包括
int main(){
std::string str=“Hello World”;
替换(str.begin(),str.end(),'l','1');
std::cout使用std::replace
#include <string>
#include <algorithm>
#include <iostream>
int main() {
std::string str = "Hello World";
std::replace(str.begin(),str.end(),'l','1');
std::cout << str; //He11o Wor1d
}
#包括
#包括
#包括
int main(){
std::string str=“Hello World”;
替换(str.begin(),str.end(),'l','1');
std::cout+1对我来说是好的解决方案!+1对我来说是好的解决方案!