3

I am trying to use set_intersection as mentioned below

std::set<std::pair<char*, int>,FileLinePairComapare > netSet;
std::set<std::pair<char*, int>,FileLinePairComapare > portSet;

std::set<std::pair<char*, int>,FileLinePairComapare> result;
std::set<std::pair<char*, int>,FileLinePairComapare>::iterator it;
std::set_intersection(netSet.begin(),netSet.end(),portSet.begin(),portSet.end(),result.begin());

I am getting compilation error on the last line

In instantiation of ‘_OIter std::set_intersection(_IIter1, _IIter1, _IIter2, _IIter2, _OIter) [with _IIter1 = std::_Rb_tree_const_iterator >; _IIter2 = std::_Rb_tree_const_iterator >; _OIter = std::_Rb_tree_const_iterator >]’:

passing ‘const std::pair’ as ‘this’ argument of ‘std::pair& std::pair::operator=(const std::pair&)’ discards qualifiers [-fpermissive]

There is no cons function where I am using these sets and set_intersection .

EdChum
  • 376,765
  • 198
  • 813
  • 562
Arghya Pal
  • 133
  • 1
  • 9
  • shouldn't you be using a std::set,FileLinePairComapare>::iteratorconst it; in your set_intersection method instead? – cageman Jul 16 '14 at 07:28

1 Answers1

9

You can't use algorithms to write to a std::set iterator directly. All set iterators are const, and for good reason because changing any of the values would corrupt the tree (the same applies to std::map keys - map iterators can only modify the mapped value).

Even if you could it wouldn't work because the container is empty (for instance if you tried to use a std::vector as the target container you'd end up with undefined behaviour).

Use a std::inserter

std::set_intersection(
  netSet.begin(), netSet.end(),
  portSet.begin(), portSet.end(),
  std::inserter(result, result.end())
);
user657267
  • 20,568
  • 5
  • 58
  • 77