std :: includes实际上做了什么?

Jus*_*tin 31 c++ language-lawyer stl-algorithm c++17

标准 来看std::includes:

返回:trueif [first2, last2)为空或者范围中的每个元素[first2, last2)都包含在范围内[first1, last1).false否则返回.

注意:由于这是在[alg.set.operations]下,因此必须对范围进行排序

从字面上看,如果我们让,R1=[first1, last1)并且R2=[first2, last2),这是在评估:

?a?R2 a?R1
Run Code Online (Sandbox Code Playgroud)

但是,这不是实际评估的内容.for R1={1}R2={1,1,1},std::includes(R1, R2)返回false:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> a({1});
    std::vector<int> b({1,1,1});

    // Outputs 'false'
    std::cout << std::boolalpha
        << std::includes(a.begin(), a.end(), b.begin(), b.end()) << '\n';
}
Run Code Online (Sandbox Code Playgroud)

住在Wandbox上

这是令人惊讶的.我用libstdc ++和libc ++验证了它,但是我认为这不是标准库实现中的一个错误,因为它是算法库的一部分.如果这不是std::includes应该运行的算法,那是什么?

Jus*_*tin 30

我在cpplang松弛中发布了这个,Casey Carter回应:

标准中的算法描述是有缺陷的.目的是确定[if]针中的每个元素按顺序出现在大海捞针中.

[它实际执行的算法是:]"如果排序的序列R1和R2的交集等于R2,则返回true"

或者,如果我们确保我们确定子序列的含义:

返回:当且仅当[first2,last2]是[first1,last1]的子序列时返回true

链接到Casey Carter的消息