Boost.Test - 如何检查两个向量是否相交?

Boost.Test - How to check two vectors on intersection?

我正在寻找检查两个向量交叉点的最佳方法。

像这样的嵌套循环

for (auto const & first: first_vector)
  for (auto const & next: next_vector)
    if first == next
      return false;

可以完成这项工作,但它看起来不像 Boost.Test 那样的方式。也不看 Boost.Test helps a lot as the only test case predefined is BOOST_CHECK_EQUAL_COLLECTIONS

更新

BOOST_FIXTURE_TEST_CASE(paths,fixture_t)
{
  for(int i=0,j=vids.size(); i!=j; i++)
  {
    for(int p=0,q=vids.size(); p!=q; p++)
    {
      if (i == p)
      {
        continue;
      }
      if (i < p)
      {
        BOOST_TEST_CONTEXT("Equal match at positions " << i << " and " << p)

        BOOST_TEST(vids[i] != vids[p]);
      }
    }
  }
}

在单元测试中,您通常不关心验证代码的性能,只要它是合理的即可。

我会写一个模板函数 sets_intersect 获取两个向量,将其中一个插入 std::hash_set 并遍历第二个向量直到第一个匹配项。或者只是对它们进行排序并使用 std::set_intersection.

检查

最终的语法将类似于 BOOST_CHECK(sets_intersect(vec1, vec2)),看起来足够人性化。