【问题标题】:How to use set_intersection with std::set in VC++?如何在 VC++ 中使用 set_intersection 和 std::set?
【发布时间】:2010-06-30 14:56:23
【问题描述】:

我正在尝试用 VC10 编译 VC6 项目... 我用 set_intersection 得到一个错误 C2678:我写了一些例子来理解。谁能解释一下如何编译这个 sn-ps ?

#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <string>

int main( )
{
    using namespace std;

    typedef set<string> MyType;

    MyType in1, in2, out;
    MyType::iterator out_iter(out.begin()); 

    set_intersection(in1.begin(),in1.end(), in2.begin(), in2.end(), out_iter);
}

输出:

c:\program files\microsoft visual\studio 10.0\vc\include\algorithm(4494): error C2678: '=' binary: 未定义运算符,它采用'const std::basic_string 类型的左操作数<_elem>'(或没有可接受的转换)

如果我使用std::vector 而不是std::set,则编译成功。 可以接受)

【问题讨论】:

  • 我完成了问题

标签: visual-c++ stl visual-c++-2010


【解决方案1】:

试试 set_intersection(in1.begin(),in1.end(), in2.begin(), in2.end(), <b>inserter(out, out.begin()) </b> );

这是因为 set_intersection 想要写入输出迭代器,导致输出容器变大。然而,这不能只用一个迭代器来完成(它可以用来覆盖现有元素但不会增加大小)

编辑:修正错字。使用插入器添加到集合中。 back_inserter 仅适用于向量等。

编辑 2:修复了另一个错字。 STL inserter 需要第二个参数,它是指向可能插入位置的提示迭代器。谢谢 chepseskaf。

【讨论】:

  • 用inserter(out, out_iter)编译成功
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 2019-07-12
  • 2014-04-15
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多