使用set_intersection()和set <int> </int>在VS2010中出错

时间:2012-08-25 17:23:28

标签: visual-c++ stl set set-intersection

我正在尝试使用set_intersection()函数。我的代码如下所示

set<int> IntersectionSet;

for(map<string, set<int>>::iterator it = mapArtist.begin() ; it != mapArtist.end() ; ++it)
{       
    for(map<string, set<int>>::iterator it2 = it ; ++it2 != mapArtist.end() ; ++it2)
    {
        set<int>::iterator iter = set_intersection(it->second.begin(), it->second.end(), it2->second.begin(), it2->second.end(), std::inserter(IntersectionSet, IntersectionSet.begin()));
        if(IntersectionSet.size() >= 300) cout << it->first << "    " << it2->first << "\n";
        IntersectionSet.clear();
    }
}

这里的问题似乎与行

有关
set<int>::iterator iter = set_intersection(it->second.begin(), it->second.end(), it2->second.begin(), it2->second.end(), std::inserter(IntersectionSet, IntersectionSet.begin()));

与我在本网站上看到的其他一些页面中使用相比,似乎是正确的。编译时出现以下错误

1>c:\users\Machine\documents\visual studio 2010\projects\artists\artists\artists.cpp(109): error C2440: 'initializing' : cannot convert from 'std::insert_iterator<_Container>' to 'std::_Tree_const_iterator<_Mytree>'
1>          with
1>          [
1>              _Container=std::set<int>
1>          ]
1>          and
1>          [
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>
1>Build FAILED.

我在这里缺少什么?感谢。

1 个答案:

答案 0 :(得分:3)

问题在于:

set<int>::iterator iter = set_intersection(/*...*/);

std::set_intersection会返回插入器,代码中的std::insert_iterator不是std::set<int>::iterator

由于您不使用iter,因此根本不需要对返回值执行任何操作;只需致电std::set_intersection