比较列表/集合元素
我希望比较两个集合,并通过迭代第一个集合来显示第二个集合中缺少的元素。
我已经使用了列表,但似乎需要遍历无序列表来查找元素。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你有两个集合:
并且你想获取其中一个集合中的元素集合,而不是另一个集合中的元素集合,则可以使用
std::set_difference
:difference
将包含所有元素位于s1
中但不在s2
中的元素。std::set_difference
适用于任意两个排序范围,因此您也可以将其与其他容器一起使用(例如,如果您的std::list
已排序,您可以对它们使用std::set_difference
来查找差异)。If you have two sets:
and you want to get the set of elements that are in one but not the other, you can use
std::set_difference
:difference
will contain all of the elements that are ins1
but not ins2
.std::set_difference
works for any two sorted ranges, so you could use it with other containers as well (e.g., if yourstd::list
s were sorted, you could usestd::set_difference
on them to find the difference).