c++ - 使用 std::find 搜索 vector 对时忽略其中一个值

标签 c++

在给定的 vector 对中

static std::vector<std::pair<int,int>> v

当我使用 std::find 搜索 vector 时,如何忽略其中一个值

std::find(v.begin(), v.end(), std::make_pair(first int, /*ignored value*/)) - v.begin();

最佳答案

使用更好的算法:std::find_if :

auto it = std::find_if(v.begin(), v.end(), [first](const std::pair<int, int>& elem){
    return elem.first == first;
});

或者来自 range-v3 的不同风格的 find :

auto it = ranges::find(v,
    first,                      // the value
    &std::pair<int, int>::first // the projection
    );

关于c++ - 使用 std::find 搜索 vector 对时忽略其中一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38488657/

相关文章:

使用 OpenMP 的 C++ 并行平均矩阵

c++ - 如何将 vector 转换为 Armadillo 矩阵?

c++ - 购买元素后最小化硬币数量

c++ - glGenFramebuffers 中的段错误

带有 '='(等号)符号的 C++ 命令行参数

c++ - C++ 中的 ObjA = ObjB

c++ - 这个 ' : MAXKEY(), root() ' 是什么,在 C++ 类中使用?

c++输出和输入单个字符

c++ - 一旦失去焦点如何更新屏幕

c++ - 我可以定义 constexpr 匿名/未命名变量吗?