What does std::includes actually do?

后端 未结 3 1030
慢半拍i
慢半拍i 2021-02-06 23:31

From the standard, std::includes:

Returns: true if [first2, last2) is empty or if every element in the range

3条回答
  •  Happy的楠姐
    2021-02-07 00:11

    As always when reading the standard, you must read the unwritten words.

    Focus on the intent not just the letter. (The wording of the standard was often found to be vague, incomplete, self-referential, or contradictory.)

    Read the introduction of section "28.7.6 Set operations on sorted structures [alg.set.operations]" :

    This subclause defines all the basic set operations on sorted structures. They also work with multisets containing multiple copies of equivalent elements. The semantics of the set operations are generalized to multisets in a standard way by defining set_­union() to contain the maximum number of occurrences of every element, set_­intersection() to contain the minimum, and so on.

    So it's perfectly clear that the words in the description of includes:

    Returns: true if [first2, last2) is empty or if every element in the range [first2, last2) is contained in the range [first1, last1). Returns false otherwise.

    must be ignored. You need to know a priori what multiset operations are, what "includes" means for two multisets, ignore the description and rebuild in your head what was the obvious intent.

    Multiset inclusion:

    A is included in B iff A union B = B.

    This is true for sets or multisets.

提交回复
热议问题