gpt4 book ai didi

c++ - 查找关于成对 vector 的条件

转载 作者:太空狗 更新时间:2023-10-29 21:07:47 31 4
gpt4 key购买 nike

假设我有一对 std::vector。我如何有效地使用方法 std::find 来查看 vector 的至少一个元素是否不等于 (false, false)?

谢谢

最佳答案

std::pair 重载 operator==,因此您可以使用 std::find 作为肯定:

bool b = std::find(v.begin(), v.end(), std::make_pair(false, false)) == v.end();

你可以使用 std::find_if 作为负数:

bool b = std::find_if(v.begin(), v.end(), 
std::bind2nd(std::not_equal_to<std::pair<bool, bool> >(),
std::make_pair(false, false)))
!= v.end();

第二个可以用 C++0x 写得更干净:

bool b = std::find_if(v.begin(), v.end(), 
[](const std::pair<bool, bool> p) {
return p != std::make_pair(false, false);
}) != v.end();

关于c++ - 查找关于成对 vector 的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4405806/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com