0

重複の可能性:
文字列内のすべての文字を置き換える方法は?

たとえば、「He​​llo World」という文字列があり、すべての「l」を「1」に置き換えたいとします。どうすればいいですか?私はc ++が初めてです。私のバックグラウンドのほとんどは Python であり、.replace メソッドを使用できます。

4

1 に答える 1

5

を使用し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
}
于 2013-01-30T03:10:52.890 に答える