0

I am trying to use the set_intersection() function. My code looks like below

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

The issue here seemsto be with the line

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

which seems to be correct compared to uses in some other pages I saw on this site. I get the following error while compiling

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.

What am I missing here? Thanks.

Chinmay Nerurkar
  • 495
  • 6
  • 22

1 Answers1

3

The problem is here:

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

std::set_intersection returns the inserter, which in your code is a std::insert_iterator, not a std::set<int>::iterator.

Since you don't use iter, you don't need to do anything with the return value at all; just call std::set_intersection.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Thanks. That did work. I got rid of the **'set::iterator iter ='** as I did not need it. The code compiles and runs. However I get a runtime error. **"Debug Assertion Falied"** **"Expression : map/set iterator is not incrementable"**. This happens in the inner for loop. – Chinmay Nerurkar Aug 25 '12 at 17:34
  • Ok. I got it. I was incrementing **it2** twice in the inner for loop. Thank you. – Chinmay Nerurkar Aug 25 '12 at 17:50