gpt4 book ai didi

c++ - 检查 STL 中的空交集

转载 作者:可可西里 更新时间:2023-11-01 17:41:57 27 4
gpt4 key购买 nike

如何检查两个 std::set 的空交集?我可以使用 set_intersection,但这太慢了,我只需要 bool 答案。

备注:std::set 表示有序集合,它们属于同一类型等。

最佳答案

自己编码有什么问题吗?

bool empty_intersection(const set<int>& x, const set<int>& y)
{
set<int>::const_iterator i = x.begin();
set<int>::const_iterator j = y.begin();
while (i != x.end() && j != y.end())
{
if (*i == *j)
return false;
else if (*i < *j)
++i;
else
++j;
}
return true;
}

反正就是这样。完全未经测试的代码。

关于c++ - 检查 STL 中的空交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12940522/

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