STL sets intersection and exit

I have a piece of code like this to compile in 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()); 

As far as I can tell, this should work. However, I get build errors:

 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> ] 

To do this, I made an explicit declaration of the template parameter:

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

But I have the same mistakes. My problem is that in the second case, if I pass const_iterator, it should end with a conversion error between const_iterator and iterator, since the type of the parameter will not match. What am I missing here? (I know about the "inserter" set_intersection form, but I want to find out what I'm doing wrong here)

+7
source share
3 answers

The output argument std::set_intersection must be changed by value_type . The std::set iterators never support mutation, since changing the value of an element can change where it belonged to the set. Functions in a group with std::set_iterator designed to work with sorted sequences, for example. std::vector .

In your case, you can either replace std::set with std::vector , sorting them as needed (and possibly using std::lower_bound and pasting them in order to sort them according to the inserts), or using std::insert_iterator( res_set, res_set.end() ) .

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

The last parameter should be an output iterator. In your case, this is not, all the more, immutable (bc. std::set has immutable elements). Instead, you should use insert_iterator:

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

res_set.begin() cannot be used as the output argument to set_intersection for two reasons:

  • The collection is empty and it will try to overwrite existing collection items.
  • You cannot change the elements of a set.

Instead, you want insert_iterator to insert new elements into the set:

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

All Articles