我有 2 个列表。让我们称它们为 A 和 B。是否有一个巧妙的 STL 技巧来查看列表 B 有哪些列表 A 没有我自己用迭代器比较元素手动循环它们?
最佳答案
The difference of two sets is formed by the elements that are present in the first set, but not in the second one. The elements copied by the function come always from the first range, in the same order.
请注意,这两个列表必须排序。
这是一个显示用法示例的示例:
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
list<int> a {1,2,3,4,5,10};
list<int> b {4,5,6,7,11};
list<int> out;
set_difference(b.begin(), b.end(), a.begin(), a.end(), back_inserter(out));
cout << "out size is " << out.size() << endl;
for(auto i: out)
{
cout << i << endl;
}
}
关于c++ - 比较列表并给出列表 b 中不在列表 a 中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533857/