【发布时间】:2010-09-25 14:22:08
【问题描述】:
我想将字符串中所有出现的 ' 替换为 ^,但我看到 string.replace 不适合我,我需要自己编写吗?很无聊。
【问题讨论】:
我想将字符串中所有出现的 ' 替换为 ^,但我看到 string.replace 不适合我,我需要自己编写吗?很无聊。
【问题讨论】:
您可以使用来自<algorithm> 的std::replace,而不是使用来自<string> 的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
【讨论】: