Taitai
Taitai

Reputation: 616

set_intersection error when computing the intersection between two sets

std::set<int> m{1,2, 4};
std::set<int> n{2,3, 4};
std::set<int> mn;
std::set<int>::iterator it;
it=set_intersection(m.begin(), m.end(),
                    n.begin(), n, end()
                    mn.begin());   //This part is not correct
for(int i : mn) cout<< i <<" ";

It seems the last parameter used in set_intersection is not correct. I tried inserter or back_inserter, but neither worked.

Upvotes: 0

Views: 123

Answers (1)

T.C.
T.C.

Reputation: 137404

std::set_intersection(m.begin(), m.end(),
                      n.begin(), n.end(),
                      std::inserter(mn, mn.begin()));

Note that you can't assign the result to a std::set<int>::iterator because the return type is actually an insert_iterator.

Upvotes: 4

Related Questions