如何在C ++中使用std :: string将一个字符替换为另一个字符? [英] How to replace one char by another using std::string in C++?

查看:47
本文介绍了如何在C ++中使用std :: string将一个字符替换为另一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C ++中使用 std :: string 将一个字符替换为另一个字符?就我而言,我正在尝试将每个 c 字符替换为 字符.

How to replace one char by another using std::string in C++? In my case, I'm trying to replace each c character by character.

我已经尝试过这种方式:

I've tried this way :

std::string str = "abcccccd";
str = str.replace('c', ' ');

但是,这行不通.

推荐答案

使用 std :: replace 算法:

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

int main()
{
  std::string str = "abccccd";
  std::replace(str.begin(), str.end(), 'c', ' ');
  std::cout << str << std::endl;
}

这篇关于如何在C ++中使用std :: string将一个字符替换为另一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆