如何获取两个字符串向量之间不同或共同元素的数量?

发布于 2024-10-20 21:10:14 字数 56 浏览 2 评论 0原文

是否有一个函数可以比较两个字符串向量以返回不同(或相同)元素的数量?我是否必须迭代它们并逐项测试?

Is there a function to compare two string vectors to return the number of different (or the same) elements? Do I have to iterate over both of them and test item by item?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风为裳 2024-10-27 21:10:14
// C++20
std::ranges::sort(v1);
std::ranges::sort(v2);
std::vector<std::string> v3;
std::ranges::set_intersection(v1, v2, std::back_inserter(v3));

// any version
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::vector<string> v3;
std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));

或者,如果您不想排序:

std::set<std::string> s1(v1.begin(), v1.end());
std::set<std::string> s2(v2.begin(), v2.end());
std::vector<std::string> v3;

// C++20
std::ranges::set_intersection(s1, s2, std::back_inserter(v3));
// any version
std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(v3));

如果向量中可能存在重复项,您可能需要使用多重集。

// C++20
std::ranges::sort(v1);
std::ranges::sort(v2);
std::vector<std::string> v3;
std::ranges::set_intersection(v1, v2, std::back_inserter(v3));

// any version
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::vector<string> v3;
std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));

Or, if you don't want to sort:

std::set<std::string> s1(v1.begin(), v1.end());
std::set<std::string> s2(v2.begin(), v2.end());
std::vector<std::string> v3;

// C++20
std::ranges::set_intersection(s1, s2, std::back_inserter(v3));
// any version
std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(v3));

You may want to use a multiset if there could be duplicates in a vector.

月亮是我掰弯的 2024-10-27 21:10:14

我不知道现有的函数,但自己编写一个应该不会太麻烦。

int compare(const vector<string>& left, const vector<string>& right) {
  auto leftIt = left.begin();
  auto rightIt = right.begin();
  auto diff = 0;
  while (leftIt != left.end() && rightIt != right.end()) {
    if (*leftIt != *rightIt) {
      diff++;
    }
    leftIt++;
    rightIt++;
  }

  // Account for different length vector instances
  if (0 == diff && (leftIt != left.end() || rightIt != right.end())) {
    diff = 1;
  }

  return diff;
}

注释

  • 为了简洁省略了 std:: 前缀
  • 如果该函数需要处理不同长度的 vector 实例,则需要更新该函数

I don't know of an existing function but writing one yourself shouldn't be too much trouble.

int compare(const vector<string>& left, const vector<string>& right) {
  auto leftIt = left.begin();
  auto rightIt = right.begin();
  auto diff = 0;
  while (leftIt != left.end() && rightIt != right.end()) {
    if (*leftIt != *rightIt) {
      diff++;
    }
    leftIt++;
    rightIt++;
  }

  // Account for different length vector instances
  if (0 == diff && (leftIt != left.end() || rightIt != right.end())) {
    diff = 1;
  }

  return diff;
}

Notes

  • Omitted std:: prefix for brevity
  • This function needs to be updated if it should handle vector<string> instances of different lengths
那请放手 2024-10-27 21:10:14

看看 set_difference()set_intersection()。在这两种情况下,您都需要事先对容器进行分类。

Have a look at set_difference() and set_intersection(). In both cases you need to have your containers sorted beforehand.

就此别过 2024-10-27 21:10:14
if (vector1 == vector2)
{
    DoSomething();
}

将根据以下链接文档对两个向量的内容进行比较:

比较两个向量的内容。

1-2) 检查lhs和rhs的内容是否相等,即它们
具有相同数量的元素,并且 lhs 中的每个元素都进行比较
与 rhs 中相同位置的元素相等。

https://en.cppreference.com/w/cpp/container/vector/运算符_cmp

if (vector1 == vector2)
{
    DoSomething();
}

The content will be compared from both vectors as per the below link documentation:

Compares the contents of two vectors.

1-2) Checks if the contents of lhs and rhs are equal, that is, they
have the same number of elements and each element in lhs compares
equal with the element in rhs at the same position.

https://en.cppreference.com/w/cpp/container/vector/operator_cmp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文