STL集的交集和输出

STL set intersection and the output

本文关键字:输出 STL      更新时间:2023-10-16

我有一个这样的代码片段,将在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.0vcincludealgorithm(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.0vcincludealgorithm(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.0vcincludealgorithm(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:p4rpkrcodedepotdevstatspokerprotypestatserverachievementmanager.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_iteror和迭代器之间出现转换错误,因为参数类型不匹配。我在这里错过了什么?(我知道set_interaction的"inserter"形式,但我想了解我在这里做错了什么)

    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()));

std::set_intersection的输出参数必须是可变的value_type。CCD_ 4的迭代器从不支持突变,因为更改元素的值可能会更改它所属的位置布景。使用std::set_iterator设计组中的功能以处理排序的序列,例如std::vector

在您的情况下,您可以将std::set替换为std::vector,根据需要对它们进行排序(并且可能使用std::lower_bound和插入以使它们在插入时保持排序),或使用CCD_ 10。

res_set.begin()不能用作set_intersection的输出参数,原因有两个:

  • 集合为空,这将尝试覆盖集合的现有元素
  • 不能修改集合的元素

相反,您需要一个insert_iterator,将新元素插入到集合中:

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