Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查STL中的空交叉点_C++_Stl_Set - Fatal编程技术网

C++ 检查STL中的空交叉点

C++ 检查STL中的空交叉点,c++,stl,set,C++,Stl,Set,如何检查两个std::sets的空交点?我可以使用set_intersection,但那太慢了,我只需要bool回答 备注:std::set表示有序集,它们是同一类型的等。您自己编码有什么问题吗 bool empty_intersection(const set<int>& x, const set<int>& y) { std<int>::const_iterator i = x.begin(); std<int>

如何检查两个
std::set
s的空交点?我可以使用
set_intersection
,但那太慢了,我只需要
bool
回答


备注:
std::set
表示有序集,它们是同一类型的等。

您自己编码有什么问题吗

bool empty_intersection(const set<int>& x, const set<int>& y)
{
    std<int>::const_iterator i = x.begin();
    std<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;
}
bool空交叉口(常数集&x,常数集&y)
{
std::const_迭代器i=x.begin();
std::const_迭代器j=y.begin();
而(i!=x.end()&&j!=y.end())
{
如果(*i==*j)
返回false;
否则,如果(*i<*j)
++一,;
其他的
++j;
}
返回true;
}

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

我只使用迭代器工作了2天,我很高兴能够做到我能做到的。好的,上面的算法与set_intersection使用的算法类似,只是如果找到一个公共元素,它会立即返回false。显然,它依赖于集合被排序的事实,因此,如果每次循环只增加低值迭代器,您将找到所有公共元素。非常感谢。我想我能读懂这篇文章,但我自己却不能编写这样的代码。你可以做一个细微的改变,删除对
操作符==
if(*id)的要求,除非问题是“我们在STL中有合适的算法吗?”