【发布时间】:2021-10-06 05:50:48
【问题描述】:
#include <algorithm>
#include <string>
void foo(std::string &s) {
replace(s.begin(), s.end(), 'S', 'TH');
}
我希望foo(s) 用两个字符TH 替换s 中的每个S。例如
std::string s = "SAT";
foo(s);
std::cout << s << "\n" // THAT
但是,foo 的定义给了我一个错误。
Error (active) E0304 no instance of function template "std::replace" 与参数列表匹配
为什么这不起作用?
【问题讨论】:
-
你使用过std命名空间吗?试试 std::replace(s.begin(), s.end(), 'S', 'TH)
-
'TH' 不是有效的语法>对于字符文字
-
@gerum 有效,有条件地支持多字符文字,它们的类型为
int:stackoverflow.com/a/34571914/4117728 -
你说得对,我修好了。