使用std::replace函数可以替换字符串中的字符。该函数有三个参数,分别是字符串的起始迭代器、字符串的结束迭代器和要替换的字符。下面是一个示例:

#include <iostream>
#include <algorithm>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    std::replace(str.begin(), str.end(), 'o', 'x');
    
    std::cout << str << std::endl;
    
    return 0;
}

输出结果为:

Hellx, Wxrld!

在这个示例中,std::replace函数将字符串str中的所有字符'o'替换为字符'x'

stdstring 替换字符

原文地址: https://www.cveoy.top/t/topic/iafa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录