c++ - 比较列表并给出列表 b 中不在列表 a 中的内容

标签 c++

我有 2 个列表。让我们称它们为 A 和 B。是否有一个巧妙的 STL 技巧来查看列表 B 有哪些列表 A 没有我自己用迭代器比较元素手动循环它们?

最佳答案

std::set_difference

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/

相关文章:

c++ - 处理 constexpr 函数中的 fatal error (或断言)

c++ - 循环中列表的最后一个元素

c++ - OpenCV 解决任意系统的返回常量值?

c++ - ifstream 在 Xcode 中不起作用?

c++ - SQLite+Qt : Select from table always returns single row

c++ - 在基于C++范围的循环中使用&运算符..令人困惑

c++ - 使用 C++ 编译器编译 Apache

c++ - 恒定时间级联计算是否可能?

c++ - Windows 上的 QTWebKit 构建错误

c# - 将 C# 移植到安卓