std::replace_if是C++标准库算法,用于在指定范围内将满足谓词条件的每个元素原地替换为给定新值;它要求容器支持前向迭代器且元素可赋值,参数为起始/结束迭代器、一元谓词和替换值。
std::replace_if 是 C++ 标准库中定义在 头文件里的算法,作用是在指定范围内,对**满足谓词条件的每个元素**,将其替换为给定的新值。它不改变容器大小,也不移动元素位置,只做原地替换。
关

std::vector、std::list、std::deque 等)std::map / std::set 的键(键不可修改)operator=),编译失败std::replace_if 必须传入三个参数:起始迭代器、结束迭代器、一个一元谓词(返回 bool),第四个参数是替换值 —— 它是**按值传递的**,不是引用,所以要注意临时对象生命周期和性能。
std::vectorv = {1, 2, 3, 4, 5, 6}; std::replace_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }, // 谓词:偶数 0); // 替换为 0 // v 变为 {1, 0, 3, 0, 5, 0}
std::vector<:string> 传 "abc" 没问题,但传 nullptr 就不行x = 42),因为传入的是副本,改了也没用这三个地方最容易出错,且错误信息往往不直观。
[&],但谓词被 std::replace_if 内部多次调用,若捕获的是局部变量地址,而该变量已出作用域 → 未定义行为const 容器(如 const std::vector& v )调用 std::replace_if → 编译失败,因为 v.begin() 返回 const_iterator,而 std::replace_if 需要可写迭代器std::replace(按值匹配)和 std::replace_if(按条件匹配)混用:前者第三个参数是“旧值”,后者第三个参数是“谓词”——顺序和语义完全不同典型错误示例:
const std::vectorv = {1,2,3}; std::replace_if(v.begin(), v.end(), [](int x){return x>1;}, 99); // ❌ 编译失败:不能通过 const_iterator 写入
当容器元素较大(如 std::string 或自定义结构体),而你只想替换部分字段,或想避免谓词中不必要的拷贝,可以用 std::ref 传引用,但注意:替换值本身仍是按值传入,std::replace_if 内部仍会调用赋值运算符。
更实用的是组合 std::bind 构建带状态的谓词(虽然 lambda 更常用):
struct IsInRange {
int low, high;
IsInRange(int l, int h) : low(l), high(h) {}
bool operator()(int x) const { return x >= low && x <= high; }
};
std::vector v = {1,5,10,15,20};
std::replace_if(v.begin(), v.end(), IsInRange{8, 12}, -1); // 替换 [8,12] 内的数为 -1
std::replace_if 不保证执行顺序,但对纯替换操作无影响;若谓词有副作用(如打印日志),顺序不可预测std::count_if,或者手写循环。