c++ - std::set_intersection 和迭代器

标签 c++ algorithm c++11 iterator c++-standard-library

<分区>

是否可以将 std::set_intersection 或其他 std::set_* 算法函数的输出直接添加到类型 std::设置?

到目前为止,我只能想办法直接添加到允许std::back_inserter的东西。

#include <set>
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;

int main()
{
   // sorted so ok for set operations
   set<int> s = { 2, 3, 1 };
   set<int> s2 = { 1 };
   vector<int> v;

   set_intersection(s.begin(), s.end(), s2.begin(), s2.end(),
      back_inserter(v));

   copy(v.begin(), v.end(), ostream_iterator<int>(cout, ","));
   return 0;
}

最佳答案

是的,您可以使用 std::inserter ,而不是像这样的 std::back_inserter

#include <iterator> // std::inserter

std::set<int> s3;
std::set_intersection(s.begin(), s.end(), s2.begin(), s2.end(),
      std::inserter(s3, s3.end()));
//    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

关于c++ - std::set_intersection 和迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63217611/

相关文章:

c++ - GCC C++ 11无法看到#if Windows和#if linux(重新询问)

c++ - 代码中void(*)()是什么意思

c++ - cout减去简单打印的输出

c++ - 如何比较 2 个 vector 并根据比较创建单独的 vector

c++ - 在继承层次结构中移动构造函数

c++ - 如何在 Qt 的主事件循环中使用 std::thread?

c++ - 使用 std::enable_if 时的对象切片

c++ - 没有 cruft 的 C++ "in"运算符?

php - 计算夜间月光时间的算法

excel - VBA Excel 中的排列