比较列表/集合元素

发布于 2024-09-05 01:12:43 字数 1220 浏览 1 评论 0原文

我希望比较两个集合,并通过迭代第一个集合来显示第二个集合中缺少的元素。

我已经使用了列表,但似乎需要遍历无序列表来查找元素。

#include <iostream>
#include <list>
using std::list;

bool isExist(list <int> &original, int i)
{
 list <int>::iterator iter;

 for (iter = original.begin(); iter != original.end(); iter++)
 {
  if (*iter == i) {
   original.splice(original.end(), original, iter);
   return true; }
 }
 return false;
}

void FindMissing(list <int> &original, list <int> &missing)
{
 int count_exist = 0;

 list <int>::iterator iter;

 for (iter = missing.begin(); iter != missing.end(); iter++)
 {if (isExist(original, *iter))
  count_exist++;}

 int count_missing = original.size() - count_exist;

 iter = original.begin();

 while(count_missing > 0)
 {
  std::cout << *iter++ << std::endl;
  count_missing--;
 }
}

int main()
{
 list <int> list_data_1;
 list <int> list_data_2;

 //Fill the list.
 for (int i = 0; i < 5; i++)
 list_data_1.push_back(i);

 //Fill second list with missing elements.
 list_data_2.push_back(3);
 list_data_2.push_back(1);
 list_data_2.push_back(4);

 FindMissing(list_data_1, list_data_2);
}

你会如何用 set 做同样的事情?

I'm looking to compare two sets and display the missing elements in second set by iterating over the first set.

I've done using lists but it seems like an overhead iterating over the unordered list to find the elements.

#include <iostream>
#include <list>
using std::list;

bool isExist(list <int> &original, int i)
{
 list <int>::iterator iter;

 for (iter = original.begin(); iter != original.end(); iter++)
 {
  if (*iter == i) {
   original.splice(original.end(), original, iter);
   return true; }
 }
 return false;
}

void FindMissing(list <int> &original, list <int> &missing)
{
 int count_exist = 0;

 list <int>::iterator iter;

 for (iter = missing.begin(); iter != missing.end(); iter++)
 {if (isExist(original, *iter))
  count_exist++;}

 int count_missing = original.size() - count_exist;

 iter = original.begin();

 while(count_missing > 0)
 {
  std::cout << *iter++ << std::endl;
  count_missing--;
 }
}

int main()
{
 list <int> list_data_1;
 list <int> list_data_2;

 //Fill the list.
 for (int i = 0; i < 5; i++)
 list_data_1.push_back(i);

 //Fill second list with missing elements.
 list_data_2.push_back(3);
 list_data_2.push_back(1);
 list_data_2.push_back(4);

 FindMissing(list_data_1, list_data_2);
}

How would you do the same with set?

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

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

发布评论

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

评论(1

好久不见√ 2024-09-12 01:12:43

如果你有两个集合:

std::set<int> s1;
std::set<int> s2;

并且你想获取其中一个集合中的元素集合,而不是另一个集合中的元素集合,则可以使用 std::set_difference

std::set<int> difference;
std::set_difference(s1.begin(), s1.end(),
                    s2.begin(), s2.end(),
                    std::inserter(difference, difference.begin()));

difference 将包含所有元素位于 s1 中但不在 s2 中的元素。

std::set_difference 适用于任意两个排序范围,因此您也可以将其与其他容器一起使用(例如,如果您的 std::list 已排序,您可以对它们使用 std::set_difference 来查找差异)。

If you have two sets:

std::set<int> s1;
std::set<int> s2;

and you want to get the set of elements that are in one but not the other, you can use std::set_difference:

std::set<int> difference;
std::set_difference(s1.begin(), s1.end(),
                    s2.begin(), s2.end(),
                    std::inserter(difference, difference.begin()));

difference will contain all of the elements that are in s1 but not in s2.

std::set_difference works for any two sorted ranges, so you could use it with other containers as well (e.g., if your std::lists were sorted, you could use std::set_difference on them to find the difference).

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