c++字符串:有没有好的方法来替换字符串中的字符

发布于 2024-09-25 01:49:36 字数 73 浏览 0 评论 0原文

我想将字符串中出现的所有 ' 替换为 ^,但我看到 string.replace 不是适合我的函数,我需要编写自己的函数吗?很无聊。

I want to replace all the occurances of ' in a string to ^, but i saw string.replace is not the right function for me, do I need to write my own? It's boring.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的笑 2024-10-02 01:49:36

您可以使用 std::replace 中的 std::replace >而不是使用 中的 string::replace

示例代码

#include <iostream>
#include <algorithm>

int main()
{
   std::string s = "I am a string";
   std::replace(s.begin(),s.end(),' ',',');
   std::cout<< s;

} 

输出:I,am,a,string< /代码>

You can use std::replace from <algorithm> instead of using string::replace from <string>

Sample code

#include <iostream>
#include <algorithm>

int main()
{
   std::string s = "I am a string";
   std::replace(s.begin(),s.end(),' ',',');
   std::cout<< s;

} 

Output : I,am,a,string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文