std::vector 删除满足某些条件的元素

std::vector removing elements which fulfill some conditions(std::vector 删除满足某些条件的元素)
本文介绍了std::vector 删除满足某些条件的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

正如标题所说,我想删除/合并满足特定条件的向量中的对象.我的意思是我知道如何从向量中删除整数,例如,其值为 99.

As the title says I want to remove/merge objects in a vector which fulfill specific conditions. I mean I know how to remove integers from a vector which have the value 99 for instance.

Scott Meyers 的 remove 成语:

The remove idiom by Scott Meyers:

vector<int> v;
v.erase(remove(v.begin(), v.end(), 99), v.end());

但是假设是否有一个包含延迟成员变量的对象向量.现在我想消除所有延迟差异仅小于特定阈值的对象,并希望将它们组合/合并为一个对象.

But suppose if have a vector of objects which contains a delay member variable. And now I want to eliminate all objects which delays differs only less than a specific threshold and want to combine/merge them to one object.

过程的结果应该是一个对象向量,其中所有延迟的差异至少应该是指定的阈值.

The result of the process should be a vector of objects where the difference of all delays should be at least the specified threshold.

推荐答案

std::remove_if来拯救!

99 将被替换为 UnaryPredicate,它会过滤您的延迟,我将使用 lambda 函数来实现.

99 would be replaced by UnaryPredicate that would filter your delays, which I am going to use a lambda function for.

这是一个例子:

v.erase(std::remove_if(
    v.begin(), v.end(),
    [](const int& x) { 
        return x > 10; // put your condition here
    }), v.end());

这篇关于std::vector 删除满足某些条件的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)