在给定的 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/