一:功能
移除序列中(满足给定条件)的元素,该操作并不是真的将元素删除,而是序列的size不变,只是更新了迭代器,该函数会返回最后一个未删除元素的位置。
二:用法
#include <vector>
#include <algorithm>
#include <iostream>int main() {std::vector<int> data{1, 2, 3, 4, 5};auto it = std::remove(data.begin(), data.end(), 3);for (auto v : std::ranges::subrange(data.begin(), it))std::cout << v << " ";std::cout << "\n";data.erase(it, data.end()); // Erase sub-rangefor (auto v : data)std::cout << v << " ";std::cout << "\n";auto is_even = [](int v) { return v % 2 == 0; };it = std::remove_if(data.begin(), data.end(), is_even);for (auto v : std::ranges::subrange(data.begin(), it))std::cout << v << " ";std::cout << "\n";data.resize(it - data.begin()); // Random Access Ranges onlyfor (auto v : data)std::cout << v << " ";std::cout << "\n";
}