-2

I am completely aware that set_difference can subtract two sets and I am aware that operators are syntactic sugars. But, I am wondering why the standard library designers do not provide such a functionality so subtraction becomes such easy?

#include <iostream>
#include <algorithm>
#include <set>

int main ()
{
  std::set<int> s1{5,10,15,20,25};
  std::set<int> s2{50,40,30,20,10};

  for (int x: s1 - s2)
    std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

instead of using this complication:

std::set_difference (s1.begin(), s1.end(), s2.begin(), s2.end(), v.begin());

Is there any implementation impediments?

Kate
  • 31
  • 2
  • 1
    Well, for starters, `s1-s2` would be a temporary. Range iteration on temporaries is a minefield of pitfalls. – Sam Varshavchik Apr 04 '20 at 23:45
  • @SamVarshavchik, my main concern is something else. you can define the third set `std::set s3 = s1 -s2;` and iterate over it. But, even that one is not possible. – Kate Apr 04 '20 at 23:51
  • 1
    Should `s1 - s2` subtract each element of `s2` from `s1`, or should it remove all elements of `s2` from `s1`? – andypea Apr 04 '20 at 23:56
  • @Kate Is the any impediments for you to implement that operator yourself? As shown in your question it's pretty much a 1-liner. c++ generally doesn't implement little convenience functions for you, especially since it's not clear exactly what `s1 - s2` would mean. – super Apr 05 '20 at 00:04
  • 2
    Mainly because subtraction of sets (or containers or arrays) can mean different things. It can mean the set of elements that are not in either set. It can mean a set created by subtracting the elements one-by-one from elements of the other (e.g. the mathematical concept of vector subtraction). The list of possibilities goes on. Whichever option is picked, there will be people complaining that another option should have been chosen. Better not to provide subtraction, and let the programmer pick a function that does what they need. – Peter Apr 05 '20 at 00:06
  • @super, I see all C++ deficiencies are justified this way. – Kate Apr 05 '20 at 00:30
  • @Kate Sure, that's one way to look at it. Another way to look at it is that you can design things in different ways. What you see as a deficiency other people see as a strength. – super Apr 05 '20 at 00:35
  • @Peter I believe this should be an answer – HTNW Apr 05 '20 at 01:00

1 Answers1

0

The set_difference algorithm operates on sequences. I cannot recall any algorithm in STL which specifically operates on a container.

STL takes great pains to separate algorithms (which operate on sequences) from containers, which store data and provide storage related operations on that data.

You are confusing the two because the algorithm name starts with a 'set'.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
  • It is not matter of or . I am wondering why there is no easy subtraction anywhere? – Kate Apr 04 '20 at 23:54
  • It is precisely that, you mixed up the algorithm with the container just because it started with the same name. Feel free to come up with your own implementation for operator - (). Btw, you are not allowed to add that implementation to `namespace std` either. – Tanveer Badar Apr 05 '20 at 05:10
  • @TanveerBadar it looks like you *mixed up* and *confused* the question not being about `set_difference` or `algorithm`, but rather what was going through the spec writers heads when they chose not to add a `-` operator to `set`. Your middle paragraph is hinting at something but it's said with little patience. – jozxyqk Feb 10 '23 at 21:50