简体   繁体   中英

Why the C++ set subtraction could not be easier?

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?

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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM