如何在VC++中使用set_intersection和std :: set?

che*_*kaf 5 stl visual-c++ visual-c++-2010

我正在尝试用VC10编译VC6项目...我用set_intersection获取了一个错误C2678:我写了一些例子来理解.任何人都可以解释如何编译这个片段?

#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);
}
Run Code Online (Sandbox Code Playgroud)

输出 :

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

如果我用一个std::vector而不是std::set编译成功.可)

rwo*_*ong 6

尝试 set_intersection(in1.begin(),in1.end(), in2.begin(), in2.end(), inserter(out, out.begin()) );

这是因为set_intersection想要写入输出迭代器,这会导致输出容器的大小增加.但是,仅使用迭代器无法完成此操作(它可用于覆盖现有元素但不会增大)

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

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