为什么 std::set_difference 不删除重复项?

Why does std::set_difference not remove duplicates?

cppreference 表示 std::set_difference:

Copies the elements from the sorted range [first1, last1) which are not found in the sorted range [first2, last2) to the range beginning at d_first.

话虽如此,我希望以下 MWE 输出 2 4,因为只有 2 和 4 不在第二组中。

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

int main() {
    std::vector<int> A{1, 1, 2, 2, 2, 3, 3, 4};
    std::set<int> B{1, 3};
    std::set<int> difference;
    std::set_difference(
        A.cbegin(), 
        A.cend(), 
        B.cbegin(),
        B.cend(), 
        std::inserter(difference, difference.end())
    );

    std::for_each(difference.cbegin(), difference.cend(), [](int n) {
        std::cout << n << " ";
    });
    std::cout << std::endl;
}

但是,这似乎输出 1 2 3 4,我不完全确定为什么。如果我将 A 更改为仅包含 1 2 3 4,它会按照我的预期进行,有人可以帮助我理解这一点吗?

我不太确定 必须 被定义为以这种方式工作是否有充分的理由,但这绝对是它的定义方式。该标准特别要求这种行为。这是 N4835 (§[set.difference]/6) 的措辞:

Remarks: If [first1, last1) contains m elements that are equivalent to each other and [first2, last2) contains n elements that are equivalent to them, the last max(m − n, 0) elements from [first1, last1) is copied to the output range, in order.