前向迭代器与多个绑定相结合的迭代速度太快
ForwardIterator combined with multiple bind's is iterating too fast
我想遍历 int
s 的vector
并删除所有偶数。
例:
std::vector<int> v = {5,2,9,3,8}
auto it = std::remove_if(v.begin(),v.end(),
std::bind(std::bind(std::equal_to<int>(),_1,0),
std::bind(std::modulus<int>(),_1,2)));
预期结果应为 {5,9,3}但它是{5,8,9,3,8}
我认为在执行绑定和删除中的所有功能之前,迭代器已经处于最后。
我知道如何以不同的方式解决它,但我想知道如何使用嵌套形式以及它如何与迭代器一起使用
在VS2015中,您的代码留下包含{5, 9, 3, 3, 8}的v
。
std::remove_if()
返回一个迭代器到vector
中第一个未使用的元素,使用它来截断vector
:
v.erase(it, v.end());
之后,v
包含 {5, 9, 3}
附带说明一下,如果你想使用 lambda 而不是 bind
你可以这样做:
std::vector<int> v = { 5, 2, 9, 3, 8 };
auto it = std::remove_if(v.begin(), v.end(), [](int val) { return val % 2 == 0; });
v.erase(it, v.end());
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 迭代时从向量和内存中删除对象
- 如何在c++迭代器类型中包装std::chrono
- 带过滤器的现代迭代c++集合
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- C++矢量迭代
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 擦除while循环中迭代的元素
- 实现一个在集合上迭代的模板函数
- 对于set上的循环-获取next元素迭代器
- 前向迭代器与多个绑定相结合的迭代速度太快
- C 程序放慢迭代速度
- 迭代速度和元素大小
- 为什么第二次迭代大量字节的速度要慢得多?以及如何解决它
- 迭代合并排序,工作速度与气泡排序相同
- c++速度比较迭代器与索引
- 计算Collatz序列的长度-自定义迭代器会导致速度变慢