【问题标题】:Error in VS2010 with set_intersection() and set<int>VS2010 中的错误 set_intersection() 和 set<int>
【发布时间】:2012-08-25 17:23:27
【问题描述】:

我正在尝试使用 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.

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

【问题讨论】:

  • 这段代码到底应该做什么?可能有比在双重嵌套循环中调用算法更简单的解决方案。
  • 我正在尝试比较一组集合中的整数集合。

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


【解决方案1】:

问题出在这里:

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

std::set_intersection 返回插入器,在您的代码中是 std::insert_iterator,而不是 std::set&lt;int&gt;::iterator

由于您不使用iter,因此您根本不需要对返回值做任何事情;只需致电std::set_intersection

【讨论】:

  • 谢谢。那确实奏效了。我摆脱了 'set::iterator iter =' 因为我不需要它。代码编译并运行。但是我得到一个运行时错误。 “调试断言失败” “表达式:map/set 迭代器不可递增”。这发生在内部 for 循环中。
  • 好的。我得到了它。我在内部 for 循环中增加了 it2 两次。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2012-06-10
相关资源
最近更新 更多