c++ - STL 设置交集和输出

标签 c++ stl set-intersection

我有一个这样的代码片段,要在 VC++ 2010 下编译。

        std::set<int> s1;
        std::set<int> s2;
        std::set<int> res_set;
        std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), res_set.begin());

据我所知,这应该有效。但是,我收到构建错误:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(4494): error C3892: 'std::_Tree_const_iterator<_Mytree>::operator *' : you cannot assign to a variable that is const
1>          with
1>          [
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(4522) : see reference to function template instantiation '_OutIt std::_Set_intersection<_InIt1,_InIt2,_OutIt>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' being compiled
1>          with
1>          [
1>              _OutIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _InIt1=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _InIt2=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(4549) : see reference to function template instantiation '_OutIt std::_Set_intersection1<std::_Tree_unchecked_const_iterator<_Mytree>,std::_Tree_unchecked_const_iterator<_Mytree>,_OutIt>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt,std::tr1::true_type)' being compiled
1>          with
1>          [
1>              _OutIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1>              _InIt1=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _InIt2=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>
1>          ]
1>          c:\p4r\pkrcode\depot\dev\stats\poker\protype\statserver\achievementmanager.cpp(175) : see reference to function template instantiation '_OutIt std::set_intersection<std::_Tree_const_iterator<_Mytree>,std::_Tree_const_iterator<_Mytree>,std::_Tree_const_iterator<_Mytree>>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' being compiled
1>          with
1>          [
1>              _OutIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1>              _InIt1=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _InIt2=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>
1>          ]

为此,我做了显式的模板参数声明:

std::set_intersection<std::set<int>::const_iterator, std::set<int>::const_iterator, std::set<int>::iterator>(
  s1.begin(), s1.end(), s2.begin(), s2.end(), res_set.begin()
);

但是我有同样的错误。我的问题是,在第二种情况下,如果我要传递一个 const_iterator,它应该会失败,并在 const_iterator 和迭代器之间出现转换错误,因为参数类型不匹配。我在这里错过了什么? (我知道 set_intersection 的“插入器”形式,但我想了解我在这里做错了什么)

最佳答案

    std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), res_set.begin());

最后一个参数应该是一个输出迭代器。在您的情况下,它甚至不是不可变的(bc. std::set 具有不可变元素)。您应该改用 insert_iterator:

    std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(res_set, res_set.end()));

关于c++ - STL 设置交集和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9160987/

相关文章:

c# - 如何执行 linq 查询以查找数据集中存在于集合的每条记录中的字段?

python - 如何正确地遍历交叉路口

c++ - boost ASIO : connection rejected on someURL's

c++ - 如何在 C++ 集合中插入值

c++ - 在循环中使用 lock_guard

c++ - 与 C 字符串 `std::string` + (`malloc` 相比, `memcpy` 的性能真的很差)

python - 查找列表之间的差异并将差异附加到列表,但对于 40 个不同的列表 - python

c++ - 使用 constexpr、SFINAE 和/或 type_traits 对 char*、char 数组和字符串文字进行重载解析

c++ - 向 avformat_open_input 添加超时

c++ - cin 一行中未定义的坐标数